getViewsData example

$access_tag = $table_data['table']['base']['access query tag'];
      $this->query->addTag($access_tag);
    }
  }

  /** * {@inheritdoc} */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    // Add the provider of the relationship's base table to the dependencies.     $table_data = $this->getViewsData()->get($this->definition['base']);
    $dependencies['module'][] = $table_data['table']['provider'];
    return $dependencies;
  }

}

/** * @} */
use Drupal\views\EntityViewsData;

/** * Provides the views data for the taxonomy entity type. */
class TermViewsData extends EntityViewsData {

  /** * {@inheritdoc} */
  public function getViewsData() {
    $data = parent::getViewsData();

    $data['taxonomy_term_field_data']['table']['base']['help'] = $this->t('Taxonomy terms are attached to nodes.');
    $data['taxonomy_term_field_data']['table']['base']['access query tag'] = 'taxonomy_term_access';
    $data['taxonomy_term_field_data']['table']['wizard_id'] = 'taxonomy_term';

    $data['taxonomy_term_field_data']['table']['join'] = [
      // This is provided for the many_to_one argument.       'taxonomy_index' => [
        'field' => 'tid',
        'left_field' => 'tid',
      ],
    ];

      $nodes[] = Node::create(['type' => 'page'] + $edit);
    }

    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $this->container->get('entity_type.manager')
      ->getStorage('node')
      ->getTableMapping();

    $current_table = $table_mapping->getDedicatedDataTableName($field_storage_string);
    $revision_table = $table_mapping->getDedicatedRevisionTableName($field_storage_string);
    $data = $this->getViewsData();

    $this->assertArrayHasKey($current_table$data);
    $this->assertArrayHasKey($revision_table$data);

    // The node field should join against node_field_data.     $this->assertArrayHasKey('node_field_data', $data[$current_table]['table']['join']);
    $this->assertArrayHasKey('node_field_revision', $data[$revision_table]['table']['join']);

    $expected_join = [
      'table' => $current_table,
      'left_field' => 'nid',
      
/** * Provides the views data for the block_content entity type. */
class BlockContentViewsData extends EntityViewsData {

  /** * {@inheritdoc} */
  public function getViewsData() {

    $data = parent::getViewsData();

    $data['block_content_field_data']['id']['field']['id'] = 'field';

    $data['block_content_field_data']['info']['field']['id'] = 'field';
    $data['block_content_field_data']['info']['field']['link_to_entity default'] = TRUE;

    $data['block_content_field_data']['type']['field']['id'] = 'field';

    $data['block_content_field_data']['table']['wizard_id'] = 'block_content';

    $data['block_content']['block_content_listing_empty'] = [
      
use Drupal\views\EntityViewsData;

/** * Provides the Views data for the media entity type. */
class MediaViewsData extends EntityViewsData {

  /** * {@inheritdoc} */
  public function getViewsData() {
    $data = parent::getViewsData();

    $data['media_field_data']['table']['wizard_id'] = 'media';
    $data['media_field_revision']['table']['wizard_id'] = 'media_revision';

    $data['media_field_data']['status_extra'] = [
      'title' => $this->t('Published status or admin user'),
      'help' => $this->t('Filters out unpublished media if the current user cannot view it.'),
      'filter' => [
        'field' => 'status',
        'id' => 'media_status',
        'label' => $this->t('Published status or admin user'),
      ],
use Drupal\views\EntityViewsData;

/** * Provides the views data for the user entity type. */
class UserViewsData extends EntityViewsData {

  /** * {@inheritdoc} */
  public function getViewsData() {
    $data = parent::getViewsData();

    $data['users_field_data']['table']['base']['help'] = $this->t('Users who have created accounts on your site.');
    $data['users_field_data']['table']['base']['access query tag'] = 'user_access';

    $data['users_field_data']['table']['wizard_id'] = 'user';

    $data['users_field_data']['uid']['argument']['id'] = 'user_uid';
    $data['users_field_data']['uid']['argument'] += [
      'name table' => 'users_field_data',
      'name field' => 'name',
      'empty field name' => \Drupal::config('user.settings')->get('anonymous'),
    ];
use Drupal\views\EntityViewsData;

/** * Provides the views data for the node entity type. */
class NodeViewsData extends EntityViewsData {

  /** * {@inheritdoc} */
  public function getViewsData() {
    $data = parent::getViewsData();

    $data['node_field_data']['table']['base']['weight'] = -10;
    $data['node_field_data']['table']['base']['access query tag'] = 'node_access';
    $data['node_field_data']['table']['wizard_id'] = 'node';

    $data['node_field_data']['nid']['argument'] = [
      'id' => 'node_nid',
      'name field' => 'title',
      'numeric' => TRUE,
      'validate type' => 'nid',
    ];

    

  }

  /** * {@inheritdoc} */
  public function getEntityType() {
    // If the user has configured a relationship on the handler take that into     // account.     if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
      $relationship = $this->displayHandler->getOption('relationships')[$this->options['relationship']];
      $table_data = $this->getViewsData()->get($relationship['table']);
      $views_data = $this->getViewsData()->get($table_data[$relationship['field']]['relationship']['base']);
    }
    else {
      $views_data = $this->getViewsData()->get($this->view->storage->get('base_table'));
    }

    if (isset($views_data['table']['entity type'])) {
      return $views_data['table']['entity type'];
    }
    else {
      throw new \Exception("No entity type for field {$this->options['id']} on view {$this->view->storage->id()}");
    }
$this->entityTypeManager->clearCachedDefinitions();

    if ($base_fields) {
      ViewsTestEntity::setMockedBaseFieldDefinitions($definition->id()$base_fields);
    }
  }

  /** * Tests base tables. */
  public function testBaseTables() {
    $data = $this->entityTypeManager->getHandler('entity_test', 'views_data')->getViewsData();

    $this->assertEquals('entity_test', $data['entity_test']['table']['entity type']);
    $this->assertEquals(FALSE, $data['entity_test']['table']['entity revision']);
    $this->assertEquals('Entity test', $data['entity_test']['table']['group']);
    $this->assertEquals('entity_test', $data['entity_test']['table']['provider']);

    $this->assertEquals('id', $data['entity_test']['table']['base']['field']);
    $this->assertEquals(['entity_test_list_cache_context']$data['entity_test']['table']['base']['cache_contexts']);
    $this->assertEquals('Entity test', $data['entity_test']['table']['base']['title']);

    // TODO: change these to assertArrayNotHasKey().
use Drupal\views\EntityViewsData;

/** * Provides views data for the comment entity type. */
class CommentViewsData extends EntityViewsData {

  /** * {@inheritdoc} */
  public function getViewsData() {
    $data = parent::getViewsData();

    $data['comment_field_data']['table']['base']['help'] = $this->t('Comments are responses to content.');
    $data['comment_field_data']['table']['base']['access query tag'] = 'comment_access';

    $data['comment_field_data']['table']['wizard_id'] = 'comment';

    $data['comment_field_data']['subject']['title'] = $this->t('Title');
    $data['comment_field_data']['subject']['help'] = $this->t('The title of the comment.');
    $data['comment_field_data']['subject']['field']['default_formatter'] = 'comment_permalink';

    $data['comment_field_data']['name']['title'] = $this->t('Author');
    
use Drupal\views\EntityViewsData;

/** * Provides views data for the file entity type. */
class FileViewsData extends EntityViewsData {

  /** * {@inheritdoc} */
  public function getViewsData() {
    $data = parent::getViewsData();

    // @TODO There is no corresponding information in entity metadata.     $data['file_managed']['table']['base']['help'] = $this->t('Files maintained by Drupal and various modules.');
    $data['file_managed']['table']['base']['defaults']['field'] = 'filename';
    $data['file_managed']['table']['wizard_id'] = 'file_managed';

    $data['file_managed']['fid']['argument'] = [
      'id' => 'file_fid',
      // The field to display in the summary.       'name field' => 'filename',
      'numeric' => TRUE,
    ];
use Drupal\views\EntityViewsData;

/** * Provides a view to override views data for test entity types. */
class EntityTestViewsData extends EntityViewsData {

  /** * {@inheritdoc} */
  public function getViewsData() {
    $views_data = parent::getViewsData();

    if ($this->entityType->id() === 'entity_test_computed_field') {
      $views_data['entity_test_computed_field']['computed_string_field'] = [
        'title' => $this->t('Computed String Field'),
        'field' => [
          'id' => 'field',
          'default_formatter' => 'string',
          'field_name' => 'computed_string_field',
        ],
      ];
    }

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