getFieldMap example


    }
    return $this->fieldMap;
  }

  /** * {@inheritdoc} */
  public function getFieldMapByFieldType($field_type) {
    if (!isset($this->fieldMapByFieldType[$field_type])) {
      $filtered_map = [];
      $map = $this->getFieldMap();
      foreach ($map as $entity_type => $fields) {
        foreach ($fields as $field_name => $field_info) {
          if ($field_info['type'] == $field_type) {
            $filtered_map[$entity_type][$field_name] = $field_info;
          }
        }
      }
      $this->fieldMapByFieldType[$field_type] = $filtered_map;
    }
    return $this->fieldMapByFieldType[$field_type];
  }

  
'test_entity_type' => [
        'id' => [
          'type' => 'integer',
          'bundles' => ['first_bundle' => 'first_bundle', 'second_bundle' => 'second_bundle'],
        ],
        'by_bundle' => [
          'type' => 'string',
          'bundles' => ['second_bundle' => 'second_bundle'],
        ],
      ],
    ];
    $this->assertEquals($expected$this->entityFieldManager->getFieldMap());
  }

  /** * @covers ::getFieldMap */
  public function testGetFieldMapFromCache() {
    $expected = [
      'test_entity_type' => [
        'id' => [
          'type' => 'integer',
          'bundles' => ['first_bundle' => 'first_bundle', 'second_bundle' => 'second_bundle'],
        ],
'bundles' => [],
      ];
    }
    $bundle_field_map[$field_name]['bundles'][$bundle] = $bundle;
    $this->keyValueFactory->get('entity.definitions.bundle_field_map')->set($entity_type_id$bundle_field_map);

    // Delete the cache entry.     $this->cacheBackend->delete('entity_field_map');

    // If the field map is initialized, update it as well, so that calls to it     // do not have to rebuild it again.     if ($field_map = $this->entityFieldManager->getFieldMap()) {
      if (!isset($field_map[$entity_type_id][$field_name])) {
        // This field did not exist yet, initialize it with the type and empty         // bundle list.         $field_map[$entity_type_id][$field_name] = [
          'type' => $field_definition->getType(),
          'bundles' => [],
        ];
      }
      $field_map[$entity_type_id][$field_name]['bundles'][$bundle] = $bundle;
      $this->entityFieldManager->setFieldMap($field_map);
    }
  }

  protected function prepareInput(array $data, ResourceType $resource_type$format, array $context) {
    $data_internal = [];

    $field_map = $this->fieldManager->getFieldMap()[$resource_type->getEntityTypeId()];

    $entity_type_id = $resource_type->getEntityTypeId();
    $entity_type_definition = $this->entityTypeManager->getDefinition($entity_type_id);
    $bundle_key = $entity_type_definition->getKey('bundle');
    $uuid_key = $entity_type_definition->getKey('uuid');

    // User resource objects contain a read-only attribute that is not a real     // field on the user entity type.     // @see \Drupal\jsonapi\JsonApiResource\ResourceObject::extractContentEntityFields()     // @todo: eliminate this special casing in https://www.drupal.org/project/drupal/issues/3079254.     if ($entity_type_id === 'user') {
      

class EntityBundleListenerTest extends EntityKernelTestBase {

  /** * @covers ::onBundleCreate * * Note: Installing the entity_schema_test module will mask the bug this test * was written to cover, as the field map cache is cleared manually by * \Drupal\Core\Field\FieldDefinitionListener::onFieldDefinitionCreate(). */
  public function testOnBundleCreate() {
    $field_map = $this->container->get('entity_field.manager')->getFieldMap();
    $expected = [
      'entity_test' => 'entity_test',
    ];
    $this->assertEquals($expected$field_map['entity_test']['id']['bundles']);

    entity_test_create_bundle('custom');
    $field_map = $this->container->get('entity_field.manager')->getFieldMap();
    $expected = [
      'entity_test' => 'entity_test',
      'custom' => 'custom',
    ];
    
$this->original = $this->entityTypeManager()
        ->getStorage('media')
        ->loadUnchanged($id);
    }

    $media_source = $this->getSource();
    foreach ($this->translations as $langcode => $data) {
      if ($this->hasTranslation($langcode)) {
        $translation = $this->getTranslation($langcode);
        // Try to set fields provided by the media source and mapped in         // media type config.         foreach ($translation->bundle->entity->getFieldMap() as $metadata_attribute_name => $entity_field_name) {
          // Only save value in the entity if the field is empty or if the           // source field changed.           if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)->isEmpty() || $translation->hasSourceFieldChanged())) {
            $translation->set($entity_field_name$media_source->getMetadata($translation$metadata_attribute_name));
          }
        }

        // Try to set a default name for this media item if no name is provided.         if ($translation->get('name')->isEmpty()) {
          $translation->setName($translation->getName());
        }

        

    else {
      $options = [MediaSourceInterface::METADATA_FIELD_EMPTY => $this->t('- Skip field -')];
      foreach ($this->entityFieldManager->getFieldDefinitions('media', $this->entity->id()) as $field_name => $field) {
        if (!($field instanceof BaseFieldDefinition) || $field_name === 'name') {
          $options[$field_name] = $field->getLabel();
        }
      }

      natcasesort($options);

      $field_map = $this->entity->getFieldMap();
      foreach ($source->getMetadataAttributes() as $metadata_attribute_name => $metadata_attribute_label) {
        $form['source_dependent']['field_map'][$metadata_attribute_name] = [
          '#type' => 'select',
          '#title' => $metadata_attribute_label,
          '#options' => $options,
          '#default_value' => $field_map[$metadata_attribute_name] ?? MediaSourceInterface::METADATA_FIELD_EMPTY,
        ];
      }
    }

    $form['additional_settings'] = [
      
$container->get('entity_field.manager'),
      $container->get('plugin.manager.field.field_type'),
      $container->get('plugin.manager.field.formatter')
    );
  }

  /** * {@inheritdoc} */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $entity_type_labels = $this->entityTypeRepository->getEntityTypeLabels();
    foreach ($this->entityFieldManager->getFieldMap() as $entity_type_id => $entity_field_map) {
      foreach ($entity_field_map as $field_name => $field_info) {
        // Skip fields without any formatters.         $options = $this->formatterManager->getOptions($field_info['type']);
        if (empty($options)) {
          continue;
        }

        foreach ($field_info['bundles'] as $bundle) {
          $derivative = $base_plugin_definition;
          $field_definitions = $this->entityFieldManager->getFieldDefinitions($entity_type_id$bundle);
          if (empty($field_definitions[$field_name])) {
            

    $this->assertFalse($entity->hasField('custom_bundle_field'));

    // Check that the custom bundle has the defined custom field and check     // saving and deleting of custom field data.     $entity = $storage->create([
      'type' => 'custom',
    ]);
    $this->assertTrue($entity->hasField('custom_bundle_field'));

    // Ensure that the field exists in the field map.     $field_map = \Drupal::service('entity_field.manager')->getFieldMap();
    $this->assertEquals(['type' => 'string', 'bundles' => ['custom' => 'custom']]$field_map['entity_test_update']['custom_bundle_field']);

    $entity->custom_bundle_field->value = 'swanky';
    $entity->save();
    $storage->resetCache();
    $entity = $storage->load($entity->id());
    $this->assertEquals('swanky', $entity->custom_bundle_field->value, 'Entity was saved correctly');

    $entity->custom_bundle_field->value = 'cozy';
    $entity->save();
    $storage->resetCache();
    

  public static function create(ContainerInterface $container) {
    /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    $entity_type_manager = $container->get('entity_type.manager');

    return new static(
      $container->get('forum_manager'),
      $entity_type_manager->getStorage('taxonomy_vocabulary'),
      $entity_type_manager->getStorage('taxonomy_term'),
      $container->get('current_user'),
      $entity_type_manager->getAccessControlHandler('node'),
      $container->get('entity_field.manager')->getFieldMap(),
      $entity_type_manager->getStorage('node_type'),
      $container->get('renderer'),
      $entity_type_manager->getDefinition('node'),
      $entity_type_manager->getDefinition('comment')
    );
  }

  /** * Returns forum page for a given forum. * * @param \Drupal\taxonomy\TermInterface $taxonomy_term * The forum to render the page for. * * @return array * A render array. */

  protected function getExistingFieldDefaults(string $field_name): array {
    $default_options = [];
    $field_map = $this->entityFieldManager->getFieldMap();

    if (empty($field_map[$this->entityTypeId][$field_name]['bundles'])) {
      return [];
    }
    $bundles = $field_map[$this->entityTypeId][$field_name]['bundles'];

    // Sort bundles to ensure deterministic behavior.     sort($bundles);
    $existing_bundle = reset($bundles);

    // Copy field configuration.

  public function getColumns() {
    $schema = $this->getSchema();
    return $schema['columns'];
  }

  /** * {@inheritdoc} */
  public function getBundles() {
    if (!$this->isDeleted()) {
      $map = \Drupal::service('entity_field.manager')->getFieldMap();
      if (isset($map[$this->getTargetEntityTypeId()][$this->getName()]['bundles'])) {
        return $map[$this->getTargetEntityTypeId()][$this->getName()]['bundles'];
      }
    }
    return [];
  }

  /** * {@inheritdoc} */
  public function getName() {
    
$loaded_media_type = $this->container->get('entity_type.manager')
      ->getStorage('media_type')
      ->load($this->testMediaType->id());
    $this->assertSame($loaded_media_type->id()$this->testMediaType->id());
    $this->assertSame($loaded_media_type->label()$new_name);
    $this->assertSame($loaded_media_type->getDescription()$new_description);
    $this->assertSame($loaded_media_type->getSource()->getPluginId(), 'test');
    $this->assertSame($loaded_media_type->getSource()->getConfiguration()['test_config_value'], 'This is new config value.');
    $this->assertTrue($loaded_media_type->shouldCreateNewRevision());
    $this->assertTrue($loaded_media_type->thumbnailDownloadsAreQueued());
    $this->assertFalse($loaded_media_type->getStatus());
    $this->assertSame($loaded_media_type->getFieldMap()['attribute_1' => 'name']);

    // We need to clear the statically cached field definitions to account for     // fields that have been created by API calls in this test, since they exist     // in a separate memory space from the web server.     $this->container->get('entity_field.manager')->clearCachedFieldDefinitions();

    // Test that a media item being created with default status to "FALSE",     // will be created unpublished.     /** @var \Drupal\media\MediaInterface $unpublished_media */
    $unpublished_media = Media::create(['name' => 'unpublished test media', 'bundle' => $loaded_media_type->id()]);
    $this->assertFalse($unpublished_media->isPublished());
    
Home | Imprint | This part of the site doesn't use cookies.