getExtraFields example

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();

    $form += [
      '#entity_type' => $this->entity->getTargetEntityTypeId(),
      '#bundle' => $this->entity->getTargetBundle(),
      '#fields' => array_keys($field_definitions),
      '#extra' => array_keys($extra_fields),
    ];

    if (empty($field_definitions) && empty($extra_fields) && $route_info = FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId()$this->entity->getTargetBundle())) {
      $this->messenger()->addWarning($this->t('There are no fields yet added. You can add new fields on the <a href=":link">Manage fields</a> page.', [':link' => $route_info->toString()]));
      return $form;
    }
$this->languageManager->getCurrentLanguage()
      ->willReturn($language)
      ->shouldBeCalledTimes(1);

    $this->cacheBackend->get($cache_id)->shouldBeCalled();

    $this->moduleHandler->invokeAll('entity_extra_field_info')->willReturn($hook_bundle_extra_fields);
    $this->moduleHandler->alter('entity_extra_field_info', $hook_bundle_extra_fields)->shouldBeCalled();

    $this->cacheBackend->set($cache_id$processed_hook_bundle_extra_fields, Cache::PERMANENT, ['entity_field_info'])->shouldBeCalled();

    $this->assertSame($processed_hook_bundle_extra_fields[$entity_type_id][$bundle]$this->entityFieldManager->getExtraFields($entity_type_id$bundle));
  }

  /** * @covers ::getFieldMap */
  public function testGetFieldMap() {
    $this->entityTypeBundleInfo->getBundleInfo('test_entity_type')->willReturn([])->shouldBeCalled();

    // Set up a content entity type.     $entity_type = $this->prophesize(ContentEntityTypeInterface::class);
    $entity_class = EntityTypeManagerTestEntity::class;

    
parent::setComponent($name$options);

    // Only continue if Layout Builder is enabled.     if (!$this->isLayoutBuilderEnabled()) {
      return $this;
    }

    // Retrieve the updated options after the parent:: call.     $options = $this->content[$name];
    // Provide backwards compatibility by converting to a section component.     $field_definition = $this->getFieldDefinition($name);
    $extra_fields = $this->entityFieldManager->getExtraFields($this->getTargetEntityTypeId()$this->getTargetBundle());
    $is_view_configurable_non_extra_field = $field_definition && $field_definition->isDisplayConfigurable('view') && isset($options['type']);
    if ($is_view_configurable_non_extra_field || isset($extra_fields['display'][$name])) {
      $configuration = [
        'label_display' => '0',
        'context_mapping' => ['entity' => 'layout_builder.entity'],
      ];
      if ($is_view_configurable_non_extra_field) {
        $configuration['id'] = 'field_block:' . $this->getTargetEntityTypeId() . ':' . $this->getTargetBundle() . ':' . $name;
        $keys = array_flip(['type', 'label', 'settings', 'third_party_settings']);
        $configuration['formatter'] = array_intersect_key($options$keys);
      }
      
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' => [
        '#markup' => 'Test1',
      ],
      'test2' => [
        '#markup' => 'Test2',
      ],
      'non_configurable_field' => [
        '#markup' => 'Non-configurable',
      ],
      

  public function processForm($element, FormStateInterface $form_state$form) {
    // Assign the weights configured in the form display.     foreach ($this->getComponents() as $name => $options) {
      if (isset($element[$name])) {
        $element[$name]['#weight'] = $options['weight'];
      }
    }

    // Hide extra fields.     $extra_fields = \Drupal::service('entity_field.manager')->getExtraFields($this->targetEntityType, $this->bundle);
    $extra_fields = $extra_fields['form'] ?? [];
    foreach ($extra_fields as $extra_field => $info) {
      if (!$this->getComponent($extra_field)) {
        $element[$extra_field]['#access'] = FALSE;
      }
    }
    return $element;
  }

  /** * {@inheritdoc} */

  protected function init() {
    // Only populate defaults for "official" view modes and form modes.     if (!$this->initialized && $this->mode !== static::CUSTOM_MODE) {
      $this->initialized = TRUE;
      $default_region = $this->getDefaultRegion();
      // Fill in defaults for extra fields.       $context = $this->displayContext == 'view' ? 'display' : $this->displayContext;
      $extra_fields = \Drupal::service('entity_field.manager')->getExtraFields($this->targetEntityType, $this->bundle);
      $extra_fields = $extra_fields[$context] ?? [];
      foreach ($extra_fields as $name => $definition) {
        if (!isset($this->content[$name]) && !isset($this->hidden[$name])) {
          // Extra fields are visible by default unless they explicitly say so.           if (!isset($definition['visible']) || $definition['visible'] == TRUE) {
            $this->setComponent($name[
              'weight' => $definition['weight'],
            ]);
          }
          else {
            $this->removeComponent($name);
          }


        return $this->loadUser($identifier$entry);
    }

    public function refreshUser(UserInterface $user): UserInterface
    {
        if (!$user instanceof LdapUser) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_debug_type($user)));
        }

        return new LdapUser($user->getEntry()$user->getUserIdentifier()$user->getPassword()$user->getRoles()$user->getExtraFields());
    }

    /** * @final */
    public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
    {
        if (!$user instanceof LdapUser) {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_debug_type($user)));
        }

        
->willReturn('foo')
        ;
        $ldap
            ->expects($this->once())
            ->method('query')
            ->willReturn($query)
        ;

        $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={user_identifier})', 'userpassword', ['memberOf']);
        $user = $provider->loadUserByIdentifier('foo');
        $this->assertInstanceOf(LdapUser::class$user);
        $this->assertSame(['memberOf' => $memberOf]$user->getExtraFields());
    }

    public function testRefreshUserShouldReturnUserWithSameProperties()
    {
        $ldap = $this->createMock(LdapInterface::class);
        $provider = new LdapUserProvider($ldap, 'ou=MyBusiness,dc=symfony,dc=com', null, null, [], 'sAMAccountName', '({uid_key}={user_identifier})', 'userpassword', ['email']);

        $user = new LdapUser(new Entry('foo'), 'foo', 'bar', ['ROLE_DUMMY']['email' => 'foo@symfony.com']);

        $this->assertEquals($user$provider->refreshUser($user));
    }
}

  public function getDerivativeDefinitions($base_plugin_definition) {
    $entity_type_labels = $this->entityTypeRepository->getEntityTypeLabels();
    foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      // Only process fieldable entity types.       if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
        continue;
      }

      $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
      foreach ($bundles as $bundle_id => $bundle) {
        $extra_fields = $this->entityFieldManager->getExtraFields($entity_type_id$bundle_id);
        // Skip bundles without any extra fields.         if (empty($extra_fields['display'])) {
          continue;
        }

        foreach ($extra_fields['display'] as $extra_field_id => $extra_field) {
          $derivative = $base_plugin_definition;

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

          $derivative['admin_label'] = $extra_field['label'];

          
protected function getEntity() {
    return $this->getContextValue('entity');
  }

  /** * {@inheritdoc} */
  public function build() {
    $entity = $this->getEntity();
    // Add a placeholder to replace after the entity view is built.     // @see layout_builder_entity_view_alter().     $extra_fields = $this->entityFieldManager->getExtraFields($entity->getEntityTypeId()$entity->bundle());
    if (!isset($extra_fields['display'][$this->fieldName])) {
      $build = [];
    }
    else {
      $build = [
        '#extra_field_placeholder_field_name' => $this->fieldName,
        // Always provide a placeholder. The Layout Builder will NOT invoke         // hook_entity_view_alter() so extra fields will not be added to the         // render array. If the hook is invoked the placeholder will be         // replaced.         // @see ::replaceFieldPlaceholder()

  protected function getFields(array $build, EntityDisplayWithLayoutInterface $display$display_context) {
    $components = $display->getComponents();

    // 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);

    
Home | Imprint | This part of the site doesn't use cookies.