isDisplayConfigurable example



  /** * Tests the display configuration settings. * * @covers ::isDisplayConfigurable * @covers ::getDisplayOptions * @dataProvider factoryTypeProvider */
  public function testDisplayConfigurationSettings($factory_name) {
    $definition = $this->initializeFieldUsingFactory($factory_name);
    $this->assertEquals(FALSE, $definition->isDisplayConfigurable('foo'));
    $this->assertEquals(NULL, $definition->getDisplayOptions('foo'));

    $definition->setDisplayConfigurable('foo', TRUE);
    $this->assertEquals(TRUE, $definition->isDisplayConfigurable('foo'));
    $this->assertEquals(['region' => 'hidden']$definition->getDisplayOptions('foo'));

    $definition->setDisplayOptions('foo', ['foo' => 'bar']);
    $this->assertEquals(['foo' => 'bar']$definition->getDisplayOptions('foo'));
  }

  /** * Provides a Mock base field default value callback. * * @param \Drupal\Core\Entity\EntityInterface $entity * Entity interface. * @param \Drupal\Core\Field\FieldDefinitionInterface $definition * Field definition. * * @return string * Default value. */


  /** * Collects the definitions of fields whose display is configurable. * * @return \Drupal\Core\Field\FieldDefinitionInterface[] * The array of field definitions */
  protected function getFieldDefinitions() {
    $context = $this->displayContext;
    return array_filter($this->entityFieldManager->getFieldDefinitions($this->entity->getTargetEntityTypeId()$this->entity->getTargetBundle())function DFieldDefinitionInterface $field_definition) use ($context) {
      return $field_definition->isDisplayConfigurable($context);
    });
  }

  /** * {@inheritdoc} */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form$form_state);

    $field_definitions = $this->getFieldDefinitions();
    $extra_fields = $this->getExtraFields();

    


    foreach ($entities as $id => $entity) {
      $build_list[$id]['_layout_builder'] = $this->buildSections($entity);

      // If there are any sections, remove all fields with configurable display       // from the existing build. These fields are replicated within sections as       // field blocks by ::setComponent().       if (!Element::isEmpty($build_list[$id]['_layout_builder'])) {
        foreach ($build_list[$id] as $name => $build_part) {
          $field_definition = $this->getFieldDefinition($name);
          if ($field_definition && $field_definition->isDisplayConfigurable($this->displayContext)) {
            unset($build_list[$id][$name]);
          }
        }
      }
    }

    return $build_list;
  }

  /** * Builds the render array for the sections of a given entity. * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity. * * @return array * The render array representing the sections of the entity. */
if (!$source_field) {
      $source_field = $source->createSourceField($media_type);
      /** @var \Drupal\field\FieldStorageConfigInterface $storage */
      $storage = $source_field->getFieldStorageDefinition();
      if ($storage->isNew()) {
        $storage->save();
      }
      $source_field->save();

      // Add the new field to the default form and view displays for this       // media type.       if ($source_field->isDisplayConfigurable('form')) {
        $display = $this->entityDisplayRepository->getFormDisplay('media', $media_type->id());
        $source->prepareFormDisplay($media_type$display);
        $display->save();
      }
      if ($source_field->isDisplayConfigurable('view')) {
        $display = $this->entityDisplayRepository->getViewDisplay('media', $media_type->id());

        // Remove all default components.         foreach (array_keys($display->getComponents()) as $name) {
          $display->removeComponent($name);
        }
        
// Ignore any extra fields from the list of field definitions. Field     // definitions can have a non-configurable display, but all extra fields are     // always displayed.     $field_definitions = array_diff_key(
      $this->entityFieldManager->getFieldDefinitions($display->getTargetEntityTypeId()$display->getTargetBundle()),
      $this->entityFieldManager->getExtraFields($display->getTargetEntityTypeId()$display->getTargetBundle())
    );

    $fields_to_exclude = array_filter($field_definitionsfunction DFieldDefinitionInterface $field_definition) use ($display_context) {
      // Remove fields with a non-configurable display.       return !$field_definition->isDisplayConfigurable($display_context);
    });
    $components = array_diff_key($components$fields_to_exclude);

    // Only include fields present in the build.     $components = array_intersect_key($components$build);

    return $components;
  }

}
/** * {@inheritdoc} */
  public function getFieldStorageDefinition() {
    return $this->getBaseFieldDefinition()->getFieldStorageDefinition();
  }

  /** * {@inheritdoc} */
  public function isDisplayConfigurable($context) {
    return $this->getBaseFieldDefinition()->isDisplayConfigurable($context);
  }

  /** * {@inheritdoc} */
  public function getDisplayOptions($display_context) {
    return $this->getBaseFieldDefinition()->getDisplayOptions($display_context);
  }

  /** * {@inheritdoc} */
$this->fieldLayoutBuilder = new FieldLayoutBuilder($this->layoutPluginManager->reveal()$this->entityFieldManager->reveal());
  }

  /** * @covers ::buildView * @covers ::getFields */
  public function testBuildView() {
    $definitions = [];
    $non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $non_configurable_field_definition->isDisplayConfigurable('view')->willReturn(FALSE);
    $definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
    $definitions['non_configurable_field_with_extra_field'] = $non_configurable_field_definition->reveal();
    $this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
    $extra_fields = [];
    $extra_fields['non_configurable_field_with_extra_field'] = [
      'label' => 'This non-configurable field is also defined in hook_entity_extra_field_info()',
    ];
    $this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn($extra_fields);

    $build = [
      'test1' => [
        
$derivative['category'] = $this->t('@entity fields', ['@entity' => $entity_type_labels[$entity_type_id]]);

          $derivative['admin_label'] = $field_definition->getLabel();

          // Add a dependency on the field if it is configurable.           if ($field_definition instanceof FieldConfigInterface) {
            $derivative['config_dependencies'][$field_definition->getConfigDependencyKey()][] = $field_definition->getConfigDependencyName();
          }
          // For any field that is not display configurable, mark it as           // unavailable to place in the block UI.           $derivative['_block_ui_hidden'] = !$field_definition->isDisplayConfigurable('view');

          $context_definition = EntityContextDefinition::fromEntityTypeId($entity_type_id)->setLabel($entity_type_labels[$entity_type_id]);
          $context_definition->addConstraint('Bundle', [$bundle]);
          $derivative['context_definitions'] = [
            'entity' => $context_definition,
            'view_mode' => new ContextDefinition('string'),
          ];

          $derivative_id = $entity_type_id . PluginBase::DERIVATIVE_SEPARATOR . $bundle . PluginBase::DERIVATIVE_SEPARATOR . $field_name;
          $this->derivatives[$derivative_id] = $derivative;
        }
      }

        }
        // Ensure extra fields have a 'region'.         if (isset($this->content[$name])) {
          $this->content[$name] += ['region' => $default_region];
        }
      }

      // Fill in defaults for fields.       $fields = $this->getFieldDefinitions();
      foreach ($fields as $name => $definition) {
        if (!$definition->isDisplayConfigurable($this->displayContext) || (!isset($this->content[$name]) && !isset($this->hidden[$name]))) {
          $options = $definition->getDisplayOptions($this->displayContext);

          if (!empty($options['region']) && $options['region'] === 'hidden') {
            $this->removeComponent($name);
          }
          elseif ($options) {
            $options += ['region' => $default_region];
            $this->setComponent($name$options);
          }
          // Note: (base) fields that do not specify display options are not           // tracked in the display at all, in order to avoid cluttering the


  /** * Tests the Media "name" base field behavior. */
  public function testNameBaseField() {
    /** @var \Drupal\Core\Field\BaseFieldDefinition[] $field_definitions */
    $field_definitions = $this->container->get('entity_field.manager')
      ->getBaseFieldDefinitions('media');

    // Ensure media name is configurable on manage display.     $this->assertTrue($field_definitions['name']->isDisplayConfigurable('view'));
    // Ensure it is not visible by default.     $this->assertSame($field_definitions['name']->getDisplayOptions('view')['region' => 'hidden']);
  }

  /** * Tests permissions based on a media type have the correct permissions. */
  public function testPermissions() {
    $permissions = $this->container->get('user.permissions')->getPermissions();
    $name = "create {$this->testMediaType->id()} media";
    $this->assertArrayHasKey($name$permissions);
    
Home | Imprint | This part of the site doesn't use cookies.