getDataTable example

// Check that the required field definitions of a translatable entity type     // exists and are stored in the correct tables.     $langcode_key = $entity_type->getKey('langcode');
    $default_langcode_key = $entity_type->getKey('default_langcode');
    $langcode_field = $this->entityDefinitionUpdateManager->getFieldStorageDefinition($langcode_key$entity_type->id());
    $default_langcode_field = $this->entityDefinitionUpdateManager->getFieldStorageDefinition($default_langcode_key$entity_type->id());
    $this->assertNotNull($langcode_field);
    $this->assertNotNull($default_langcode_field);

    $database_schema = $this->database->schema();
    $base_table = $entity_type->getBaseTable();
    $data_table = $entity_type->getDataTable();
    $this->assertTrue($database_schema->tableExists($data_table));

    $this->assertTrue($database_schema->fieldExists($base_table$langcode_key));
    $this->assertTrue($database_schema->fieldExists($data_table$langcode_key));

    $this->assertFalse($database_schema->fieldExists($base_table$default_langcode_key));
    $this->assertTrue($database_schema->fieldExists($data_table$default_langcode_key));
  }

  /** * Asserts the revisionable / translatable characteristics of an entity type. * * @internal */

    elseif ($entity_type->isRevisionable() && $entity_type->getRevisionTable() != $original->getRevisionTable()) {
      $changes[] = static::REVISION_TABLE_RENAME;
    }

    if ($translation_add) {
      $changes[] = static::DATA_TABLE_ADDITION;
    }
    elseif ($translation_remove) {
      $changes[] = static::DATA_TABLE_REMOVAL;
    }
    elseif ($entity_type->isTranslatable() && $entity_type->getDataTable() != $original->getDataTable()) {
      $changes[] = static::DATA_TABLE_RENAME;
    }

    if ($entity_type->isRevisionable() && $entity_type->isTranslatable()) {
      if ($revision_add || $translation_add) {
        $changes[] = static::REVISION_DATA_TABLE_ADDITION;
      }
      elseif ($entity_type->getRevisionDataTable() != $original->getRevisionDataTable()) {
        $changes[] = static::REVISION_DATA_TABLE_RENAME;
      }
    }
    
/** * Defines the comment schema handler. */
class CommentStorageSchema extends SqlContentEntityStorageSchema {

  /** * {@inheritdoc} */
  protected function getEntitySchema(ContentEntityTypeInterface $entity_type$reset = FALSE) {
    $schema = parent::getEntitySchema($entity_type$reset);

    if ($data_table = $this->storage->getDataTable()) {
      $schema[$data_table]['indexes'] += [
        'comment__status_pid' => ['pid', 'status'],
        'comment__num_new' => [
          'entity_id',
          'entity_type',
          'comment_type',
          'status',
          'created',
          'cid',
          'thread',
        ],
        
/** * Defines the node schema handler. */
class NodeStorageSchema extends SqlContentEntityStorageSchema {

  /** * {@inheritdoc} */
  protected function getEntitySchema(ContentEntityTypeInterface $entity_type$reset = FALSE) {
    $schema = parent::getEntitySchema($entity_type$reset);

    if ($data_table = $this->storage->getDataTable()) {
      $schema[$data_table]['indexes'] += [
        'node__frontpage' => ['promote', 'status', 'sticky', 'created'],
        'node__title_type' => ['title', ['type', 4]],
      ];
    }

    return $schema;
  }

  /** * {@inheritdoc} */
$this->entityType = $entity_type;
    $this->fieldStorageDefinitions = $storage_definitions;
    $this->prefix = $prefix;

    // @todo Remove table names from the entity type definition in     // https://www.drupal.org/node/2232465.     $this->baseTable = $this->prefix . $entity_type->getBaseTable() ?: $entity_type->id();
    if ($entity_type->isRevisionable()) {
      $this->revisionTable = $this->prefix . $entity_type->getRevisionTable() ?: $entity_type->id() . '_revision';
    }
    if ($entity_type->isTranslatable()) {
      $this->dataTable = $this->prefix . $entity_type->getDataTable() ?: $entity_type->id() . '_field_data';
    }
    if ($entity_type->isRevisionable() && $entity_type->isTranslatable()) {
      $this->revisionDataTable = $this->prefix . $entity_type->getRevisionDataTable() ?: $entity_type->id() . '_field_revision';
    }
  }

  /** * Initializes the table mapping. * * @param \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type * The entity type definition. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface[] $storage_definitions * A list of field storage definitions that should be available for the * field columns of this table mapping. * @param string $prefix * (optional) A prefix to be used by all the tables of this mapping. * Defaults to an empty string. * * @return static * * @internal */

  protected function assertItemsTableCount(int $count, EntityTypeInterface $definition): void {
    $connection = Database::getConnection();
    $this->assertEquals(1, (int) $connection->select($definition->getBaseTable())->countQuery()->execute()->fetchField());
    $this->assertEquals(1, (int) $connection->select($definition->getDataTable())->countQuery()->execute()->fetchField());
    $this->assertEquals($count(int) $connection->select($definition->getRevisionTable())->countQuery()->execute()->fetchField());
    $this->assertEquals($count(int) $connection->select($definition->getRevisionDataTable())->countQuery()->execute()->fetchField());

  }

  /** * Creates a new revision in the entity of this test class. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity where revision will be created. * @param \Drupal\user\UserInterface $user * The author of the new revision. * @param int $timestamp * The timestamp of the new revision. * @param string $log_message * The log message of the new revision. */
$view_settings = $this->getConfiguration()['view'];
    $displays = Views::getApplicableViews('entity_reference_display');
    // Filter views that list the entity type we want, and group the separate     // displays by view.     $entity_type = $this->entityTypeManager->getDefinition($this->configuration['target_type']);
    $view_storage = $this->entityTypeManager->getStorage('view');

    $options = [];
    foreach ($displays as $data) {
      [$view_id$display_id] = $data;
      $view = $view_storage->load($view_id);
      if (in_array($view->get('base_table')[$entity_type->getBaseTable()$entity_type->getDataTable()])) {
        $display = $view->get('display');
        $options[$view_id . ':' . $display_id] = $view_id . ' - ' . $display[$display_id]['display_title'];
      }
    }

    // The value of the 'view_and_display' select below will need to be split     // into 'view_name' and 'view_display' in the final submitted values, so     // we massage the data at validate time on the wrapping element (not     // ideal).     $form['view']['#element_validate'] = [[static::class, 'settingsFormValidate']];

    

  public function __construct(array $configuration$plugin_id$plugin_definition, EntityTypeBundleInfoInterface $bundle_info_service, MenuParentFormSelectorInterface $parent_form_selector) {
    parent::__construct($configuration$plugin_id$plugin_definition);

    $this->bundleInfoService = $bundle_info_service;
    $this->base_table = $this->definition['base_table'];

    $this->parentFormSelector = $parent_form_selector;

    $entity_types = \Drupal::entityTypeManager()->getDefinitions();
    foreach ($entity_types as $entity_type_id => $entity_type) {
      if (in_array($this->base_table, [$entity_type->getBaseTable()$entity_type->getDataTable()$entity_type->getRevisionTable()$entity_type->getRevisionDataTable()], TRUE)) {
        $this->entityType = $entity_type;
        $this->entityTypeId = $entity_type_id;
      }
    }
  }

  /** * Gets the createdColumn property. * * @return string * The name of the column containing the created date. */

  public function loadTree($vid$parent = 0, $max_depth = NULL, $load_entities = FALSE) {
    $cache_key = implode(':', func_get_args());
    if (!isset($this->trees[$cache_key])) {
      // We cache trees, so it's not CPU-intensive to call on a term and its       // children, too.       if (!isset($this->treeChildren[$vid])) {
        $this->treeChildren[$vid] = [];
        $this->treeParents[$vid] = [];
        $this->treeTerms[$vid] = [];
        $query = $this->database->select($this->getDataTable(), 't');
        $query->join('taxonomy_term__parent', 'p', '[t].[tid] = [p].[entity_id]');
        $query->addExpression('[parent_target_id]', 'parent');
        $result = $query
          ->addTag('taxonomy_term_access')
          ->fields('t')
          ->condition('t.vid', $vid)
          ->condition('t.default_langcode', 1)
          ->orderBy('t.weight')
          ->orderBy('t.name')
          ->execute();
        foreach ($result as $term) {
          
// ensureEntityTable() decides whether an entity property will be         // queried from the data table or the base table based on where it         // finds the property first. The data table is preferred, which is why         // it gets added before the base table.         $entity_tables = [];
        $revision_table = NULL;
        if ($query_revisions) {
          $data_table = $entity_type->getRevisionDataTable();
          $entity_base_table = $entity_type->getRevisionTable();
        }
        else {
          $data_table = $entity_type->getDataTable();
          $entity_base_table = $entity_type->getBaseTable();

          if ($field_storage && $field_storage->isRevisionable() && in_array($field_storage->getName()$entity_type->getRevisionMetadataKeys())) {
            $revision_table = $entity_type->getRevisionTable();
          }
        }
        if ($data_table) {
          $this->sqlQuery->addMetaData('simple_query', FALSE);
          $entity_tables[$data_table] = $this->getTableMapping($data_table$entity_type_id);
        }
        if ($revision_table) {
          
// Provide a relationship for each entity type except comment.     foreach ($entities_types as $type => $entity_type) {
      if ($type == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class) || !$entity_type->getBaseTable()) {
        continue;
      }
      if (\Drupal::service('comment.manager')->getFields($type)) {
        $data['comment_field_data'][$type] = [
          'relationship' => [
            'title' => $entity_type->getLabel(),
            'help' => $this->t('The @entity_type to which the comment is a reply to.', ['@entity_type' => $entity_type->getLabel()]),
            'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
            'base field' => $entity_type->getKey('id'),
            'relationship field' => 'entity_id',
            'id' => 'standard',
            'label' => $entity_type->getLabel(),
            'extra' => [
              [
                'field' => 'entity_type',
                'value' => $type,
                'table' => 'comment_field_data',
              ],
            ],
          ],

  public function getDerivativeDefinitions($base_plugin_definition) {
    foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
      // Just add support for entity types which have a views integration.       if (($base_table = $entity_type->getBaseTable()) && $this->viewsData->get($base_table) && $this->entityTypeManager->hasHandler($entity_type_id, 'view_builder')) {
        $this->derivatives[$entity_type_id] = [
          'id' => 'entity:' . $entity_type_id,
          'provider' => 'views',
          'title' => $entity_type->getLabel(),
          'help' => $this->t('Display the @label', ['@label' => $entity_type->getLabel()]),
          'base' => [$entity_type->getDataTable() ?: $entity_type->getBaseTable()],
          'entity_type' => $entity_type_id,
          'display_types' => ['normal'],
          'class' => $base_plugin_definition['class'],
        ];
      }
    }

    return $this->derivatives;
  }

}

  protected function assertFieldAccess($entity_type_id$field_name$field_content) {
    \Drupal::state()->set('views_field_access_test-field', $field_name);

    $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
    $view_id = $this->randomMachineName();
    $data_table = $entity_type->getDataTable();
    // Use the data table as long as the field is not 'uuid'. This is the only     // column that can only be obtained from the base table.     $base_table = ($data_table && ($field_name !== 'uuid')) ? $data_table : $entity_type->getBaseTable();
    $entity = View::create([
      'id' => $view_id,
      'base_table' => $base_table,
      'display' => [
        'default' => [
          'display_plugin' => 'default',
          'id' => 'default',
          'display_options' => [
            
$schema = parent::getEntitySchema($entity_type$reset);

    // Creates unique keys to guarantee the integrity of the entity and to make     // the lookup in ModerationStateFieldItemList::getModerationState() fast.     $unique_keys = [
      'content_entity_type_id',
      'content_entity_id',
      'content_entity_revision_id',
      'workflow',
      'langcode',
    ];
    if ($data_table = $this->storage->getDataTable()) {
      $schema[$data_table]['unique keys'] += [
        'content_moderation_state__lookup' => $unique_keys,
      ];
    }
    if ($revision_data_table = $this->storage->getRevisionDataTable()) {
      $schema[$revision_data_table]['unique keys'] += [
        'content_moderation_state__lookup' => $unique_keys,
      ];
    }

    return $schema;
  }
->method('isTranslatable')
      ->willReturn(TRUE);
    $this->entityType->expects($this->exactly(1))
      ->method('getDataTable')
      ->willReturn('entity_test_field_data');
    $this->entityType->expects($this->any())
      ->method('getRevisionMetadataKeys')
      ->willReturn([]);

    $this->setUpEntityStorage();

    $this->assertSame('entity_test_field_data', $this->entityStorage->getDataTable());
  }

  /** * Tests SqlContentEntityStorage::getRevisionDataTable(). * * @param string $revision_data_table * The revision data table to be returned by the mocked entity type. * @param string $expected * The expected return value of * SqlContentEntityStorage::getRevisionDataTable(). * * @covers ::__construct * @covers ::getRevisionDataTable * * @dataProvider providerTestGetRevisionDataTable */
Home | Imprint | This part of the site doesn't use cookies.