getBaseFieldDefinitions example

$source = $stringStorage->createString([
      'source' => 'Revision ID',
    ])->save();

    $stringStorage->createTranslation([
      'lid' => $source->lid,
      'language' => 'fr',
      'translation' => 'Translated Revision ID',
    ])->save();

    // Ensure that the field is translated when access through the API.     $this->assertEquals('Translated Revision ID', \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('node')['vid']->getLabel());

    // Assert there are no updates.     $this->assertFalse(\Drupal::service('entity.definition_update_manager')->needsUpdates());
  }

  /** * Tests that translations do not affect the update system. */
  public function testTranslatedUpdate() {
    // Visit the update page to collect any strings that may be translatable.     $user = $this->drupalCreateUser(['administer software updates']);
    


  /** * {@inheritdoc} */
  public function fields() {
    // Retrieving fields from a non-fieldable content entity will throw a     // LogicException. Return an empty list of fields instead.     if (!$this->entityType->entityClassImplements('Drupal\Core\Entity\FieldableEntityInterface')) {
      return [];
    }
    $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType->id());
    if (!empty($this->configuration['bundle'])) {
      $field_definitions += $this->entityFieldManager->getFieldDefinitions($this->entityType->id()$this->configuration['bundle']);
    }
    $fields = array_map(function D$definition) {
      return (string) $definition->getLabel();
    }$field_definitions);
    return $fields;
  }

  /** * {@inheritdoc} */


    // Create an entity to test access against.     $node = Node::create([
      'title' => 'dummy_title',
      'type' => 'article',
      'uid' => 1,
    ]);

    // While the method is only used to check file fields it should work without     // error for any field whether it is a base field or a bundle field.     $base_field_definition = $this->container->get('entity_field.manager')->getBaseFieldDefinitions('node')['title'];
    $bundle_field_definition = $this->container->get('entity_field.manager')->getFieldDefinitions('node', 'article')['field_relationships'];

    // Tests the expected access result for each user.     // The $article_editor account can edit any article.     $result = TemporaryJsonapiFileFieldUploader::checkFileUploadAccess($article_editor$base_field_definition$node);
    $this->assertTrue($result->isAllowed());
    // The article editor cannot create a node of undetermined type.     $result = TemporaryJsonapiFileFieldUploader::checkFileUploadAccess($article_editor$base_field_definition);
    $this->assertFalse($result->isAllowed());
    // The article editor can edit any article.     $result = TemporaryJsonapiFileFieldUploader::checkFileUploadAccess($article_editor$bundle_field_definition$node);
    
// Assign the ID.     $this->id = $this->id();

    // Field name cannot be longer than FieldStorageConfig::NAME_MAX_LENGTH     // characters. We use mb_strlen() because the DB layer assumes that column     // widths are given in characters rather than bytes.     if (mb_strlen($this->getName()) > static::NAME_MAX_LENGTH) {
      throw new FieldException('Attempt to create a field storage with an name longer than ' . static::NAME_MAX_LENGTH . ' characters: ' . $this->getName());
    }

    // Disallow reserved field names.     $disallowed_field_names = array_keys($entity_field_manager->getBaseFieldDefinitions($this->getTargetEntityTypeId()));
    if (in_array($this->getName()$disallowed_field_names)) {
      throw new FieldException("Attempt to create field storage {$this->getName()} which is reserved by entity type {$this->getTargetEntityTypeId()}.");
    }

    // Check that the field type is known.     $field_type = $field_type_manager->getDefinition($this->getType(), FALSE);
    if (!$field_type) {
      throw new FieldException("Attempt to create a field storage of unknown type {$this->getType()}.");
    }
    $this->module = $field_type['provider'];

    

  protected function setUp(): void {
    parent::setUp();
    $this->viewsData = $this->container->get('views.views_data');
    $this->entityFieldManager = $this->container->get('entity_field.manager');
  }

  /** * Tests if user views data object doesn't contain pass field. */
  public function testUserPasswordFieldNotAvailableToViews() {
    $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions('user');
    $this->assertArrayHasKey('pass', $field_definitions);
    $this->assertArrayNotHasKey('pass', $this->viewsData->get('users_field_data'));
  }

}
/** * Tests the getBaseFieldDefinitions() method. * * @covers ::getBaseFieldDefinitions * @covers ::buildBaseFieldDefinitions */
  public function testGetBaseFieldDefinitions() {
    $field_definition = $this->setUpEntityWithFieldDefinition();

    $expected = ['id' => $field_definition];
    $this->assertSame($expected$this->entityFieldManager->getBaseFieldDefinitions('test_entity_type'));
  }

  /** * Tests the getFieldDefinitions() method. * * @covers ::getFieldDefinitions * @covers ::buildBundleFieldDefinitions */
  public function testGetFieldDefinitions() {
    $field_definition = $this->setUpEntityWithFieldDefinition();

    

  protected function getDefinitionFromEntity($key) {
    $plugin_id = $this->getPluginId();
    $entity_type_id = $this->getEntityTypeId($plugin_id);
    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $definitions */
    $definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
    $field_definition = $definitions[$key];

    return [
      'type' => $field_definition->getType(),
    ] + $field_definition->getSettings();
  }

  /** * Finds the entity type from configuration or plugin ID. * * @param string $plugin_id * The plugin ID. * * @return string * The entity type. */

  protected function extractBundleData(array &$data, EntityTypeInterface $entity_type_definition) {
    $bundle_key = $entity_type_definition->getKey('bundle');
    // Get the base field definitions for this entity type.     $base_field_definitions = $this->getEntityFieldManager()->getBaseFieldDefinitions($entity_type_definition->id());

    // Get the ID key from the base field definition for the bundle key or     // default to 'value'.     $key_id = isset($base_field_definitions[$bundle_key]) ? $base_field_definitions[$bundle_key]->getFieldStorageDefinition()->getMainPropertyName() : 'value';

    // Normalize the bundle if it is not explicitly set.     $bundle_value = $data[$bundle_key][0][$key_id] ?? ($data[$bundle_key] ?? NULL);
    // Unset the bundle from the data.     unset($data[$bundle_key]);

    // Get the bundle entity type from the entity type definition.
$entity_type_manager = $this->container->get('entity_type.manager');

    /** @var \Drupal\Core\Field\BaseFieldDefinition[][] $field_definitions */
    $field_definitions = [];

    /** @var \Drupal\Core\Entity\EntityTypeInterface[] $content_entity_types */
    $content_entity_types = array_filter($entity_type_manager->getDefinitions()function DEntityTypeInterface $entity_type) {
      return $entity_type instanceof ContentEntityTypeInterface;
    });

    foreach ($content_entity_types as $entity_type_id => $entity_type_definition) {
      $field_definitions[$entity_type_id] = $entity_field_manager->getBaseFieldDefinitions($entity_type_id);
    }

    foreach ($field_definitions as $entity_type_id => $definitions) {
      foreach ($definitions as $field_id => $field_definition) {
        $this->checkDisplayOption($entity_type_id$field_id$field_definition$field_formatter_manager, 'view');
        $this->checkDisplayOption($entity_type_id$field_id$field_definition$field_widget_manager, 'form');
      }
    }
  }

  /** * Helper method that tries to load plugin definitions. * * @param string $entity_type_id * Id of entity type. Required by message. * @param string $field_id * Id of field. Required by message. * @param \Drupal\Core\Field\BaseFieldDefinition $field_definition * Field definition that provide display options. * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $plugin_manager * Plugin manager that will try to provide plugin definition. * @param string $display_context * Defines which display options should be loaded. */


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

    $type = $this->entity;
    if ($this->operation == 'add') {
      $form['#title'] = $this->t('Add content type');
      $fields = $this->entityFieldManager->getBaseFieldDefinitions('node');
      // Create a node with a fake bundle using the type's UUID so that we can       // get the default values for workflow settings.       // @todo Make it possible to get default values without an entity.       // https://www.drupal.org/node/2318187       $node = $this->entityTypeManager->getStorage('node')->create(['type' => $type->uuid()]);
    }
    else {
      $form['#title'] = $this->t('Edit %label content type', ['%label' => $type->label()]);
      $fields = $this->entityFieldManager->getFieldDefinitions('node', $type->id());
      // Create a node to get the current values for workflow settings fields.       $node = $this->entityTypeManager->getStorage('node')->create(['type' => $type->id()]);
    }
$this->entityType->getPluralLabel()->willReturn('bar');
    $this->storage->getEntityType()->willReturn($this->entityType->reveal());
    $this->storage->getEntityTypeId()->willReturn('foo');
  }

  /** * Tests that revision destination fails for unrevisionable entities. */
  public function testUnrevisionable() {
    $this->entityType->getKey('id')->willReturn('id');
    $this->entityType->getKey('revision')->willReturn('');
    $this->entityFieldManager->getBaseFieldDefinitions('foo')
      ->willReturn([
        'id' => BaseFieldDefinitionTest::create('integer'),
      ]);

    $destination = new EntityRevisionTestDestination(
      [],
      '',
      [],
      $this->migration->reveal(),
      $this->storage->reveal(),
      [],
      


    return $base_field_definitions;
  }

  /** * {@inheritdoc} */
  public function getFieldDefinitions($entity_type_id$bundle) {
    $langcode = $this->languageManager->getCurrentLanguage()->getId();
    if (!isset($this->fieldDefinitions[$entity_type_id][$bundle][$langcode])) {
      $base_field_definitions = $this->getBaseFieldDefinitions($entity_type_id);
      // Not prepared, try to load from cache.       $cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $langcode;
      if ($cache = $this->cacheGet($cid)) {
        $bundle_field_definitions = $cache->data;
      }
      else {
        // Rebuild the definitions and put it into the cache.         $bundle_field_definitions = $this->buildBundleFieldDefinitions($entity_type_id$bundle$base_field_definitions);
        $this->cacheSet($cid$bundle_field_definitions, Cache::PERMANENT, ['entity_types', 'entity_field_info']);
      }
      // Field definitions consist of the bundle specific overrides and the
// Entities are complex data.     $this->assertNotInstanceOf(ListDataDefinitionInterface::class$entity_definition);
    $this->assertInstanceOf(ComplexDataDefinitionInterface::class$entity_definition);

    // Entity definitions should inherit their labels from the entity type.     $this->assertEquals('Content', $entity_definition->getLabel());
    $this->assertEquals('Article', $bundle_definition->getLabel());

    $field_definitions = $entity_definition->getPropertyDefinitions();
    // Comparison should ignore the internal static cache, so compare the     // serialized objects instead.     $this->assertEquals(serialize(\Drupal::service('entity_field.manager')->getBaseFieldDefinitions('node'))serialize($field_definitions));
    $this->assertEquals('field_item:string', $entity_definition->getPropertyDefinition('title')->getItemDefinition()->getDataType());
    $this->assertNull($entity_definition->getMainPropertyName());
    $this->assertNull($entity_definition->getPropertyDefinition('invalid'));

    $entity_definition2 = $this->typedDataManager->createDataDefinition('entity:node');
    $this->assertNotInstanceOf(ListDataDefinitionInterface::class$entity_definition2);
    $this->assertInstanceOf(ComplexDataDefinitionInterface::class$entity_definition2);
    $this->assertEquals(serialize($entity_definition)serialize($entity_definition2));

    // Test that the definition factory creates the right definitions for all     // entity data types variants.
if (!in_array('Drupal\Core\Entity\FieldableEntityInterface', class_implements($entity_type_class))) {
          $this->propertyDefinitions = [];
        }
        else {
          // @todo: Add support for handling multiple bundles.           // See https://www.drupal.org/node/2169813.           $bundles = $this->getBundles();
          if (is_array($bundles) && count($bundles) == 1) {
            $this->propertyDefinitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_idreset($bundles));
          }
          else {
            $this->propertyDefinitions = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($entity_type_id);
          }
        }
      }
      else {
        // No entity type given.         $this->propertyDefinitions = [];
      }
    }
    return $this->propertyDefinitions;
  }

  


    }

    $this->addEntityLinks($data[$base_table]);
    if ($views_revision_base_table) {
      $this->addEntityLinks($data[$views_revision_base_table]);
    }

    // Load all typed data definitions of all fields. This should cover each of     // the entity base, revision, data tables.     $field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entityType->id());
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $this->storage->getTableMapping($field_definitions);
    // Fetch all fields that can appear in both the base table and the data     // table.     $duplicate_fields = array_intersect_key($entity_keysarray_flip(['id', 'revision', 'bundle']));
    // Iterate over each table we have so far and collect field data for each.     // Based on whether the field is in the field_definitions provided by the     // entity field manager.     // @todo We should better just rely on information coming from the entity     // storage.     // @todo https://www.drupal.org/node/2337511
Home | Imprint | This part of the site doesn't use cookies.