Commit 35b3d0fa authored by Alex Pott's avatar Alex Pott
Browse files

Issue #1740492 by dawehner, damiankloip, dasjo, xjm: Implement a default entity views data handler.

parent 7f69cb44
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ class SqlContentEntityStorageSchema implements EntitySchemaHandlerInterface {
  /**
   * The storage field definitions for this entity type.
   *
   * @var \Drupal\Core\Field\FieldDefinitionInterface[]
   * @var \Drupal\Core\Field\FieldStorageDefinitionInterface[]
   */
  protected $fieldStorageDefinitions;

+29 −166
Original line number Diff line number Diff line
@@ -7,53 +7,32 @@

namespace Drupal\file;

use Drupal\views\EntityViewsDataInterface;
use Drupal\views\EntityViewsData;

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

  /**
   * {@inheritdoc}
   */
  public function getViewsData() {
    $data = array();
    // Sets 'group' index for file_managed table.
    $data['file_managed']['table']['group']  = t('File');
    $data = parent::getViewsData();

    // Advertise this table as a possible base table.
    $data['file_managed']['table']['base'] = array(
      'field' => 'fid',
      'title' => t('File'),
      'help' => t("Files maintained by Drupal and various modules."),
      'defaults' => array(
        'field' => 'filename'
      ),
    );
    $data['file_managed']['table']['entity type'] = 'file';
    // @TODO There is no corresponding information in entity metadata.
    $data['file_managed']['table']['base']['help'] = t('Files maintained by Drupal and various modules.');
    $data['file_managed']['table']['base']['defaults']['field'] = 'filename';
    $data['file_managed']['table']['wizard_id'] = 'file_managed';

    // Describes fid field in file_managed table.
    $data['file_managed']['fid'] = array(
      'title' => t('File ID'),
      'help' => t('The ID of the file.'),
      'field' => array(
        'id' => 'file',
      ),
      'argument' => array(
    $data['file_managed']['fid']['field']['id'] ='file';
    $data['file_managed']['fid']['argument'] = array(
      'id' => 'file_fid',
      // The field to display in the summary.
      'name field' => 'filename',
      'numeric' => TRUE,
      ),
      'filter' => array(
        'id' => 'numeric',
      ),
      'sort' => array(
        'id' => 'standard',
      ),
      'relationship' => array(
    );
    $data['file_managed']['fid']['relationship'] = array(
      'title' => t('File usage'),
      'help' => t('Relate file entities to their usage.'),
      'id' => 'standard',
@@ -61,64 +40,14 @@ public function getViewsData() {
      'base field' => 'fid',
      'field' => 'fid',
      'label' => t('File usage'),
      ),
    );

    // Describes filename field in file_managed table.
    $data['file_managed']['filename'] = array(
      'title' => t('Name'),
      'help' => t('The name of the file.'),
      'field' => array(
        'id' => 'file',
       ),
      'sort' => array(
        'id' => 'standard',
      ),
      'filter' => array(
        'id' => 'string',
      ),
      'argument' => array(
        'id' => 'string',
      ),
    );
    $data['file_managed']['filename']['field']['id'] = 'file';

    // Describes uri field in file_managed table.
    $data['file_managed']['uri'] = array(
      'title' => t('Path'),
      'help' => t('The path of the file.'),
      'field' => array(
        'id' => 'file_uri',
       ),
      'sort' => array(
        'id' => 'standard',
      ),
      'filter' => array(
        'id' => 'string',
      ),
      'argument' => array(
        'id' => 'string',
      ),
    );
    $data['file_managed']['uri']['field']['id'] = 'file_uri';

    // Describes filemime field in file_managed table.
    $data['file_managed']['filemime'] = array(
      'title' => t('Mime type'),
      'help' => t('The mime type of the file.'),
      'field' => array(
        'id' => 'file_filemime',
       ),
      'sort' => array(
        'id' => 'standard',
      ),
      'filter' => array(
        'id' => 'string',
      ),
      'argument' => array(
        'id' => 'string',
      ),
    );
    $data['file_managed']['filemime']['field']['id'] = 'file_filemime';

    // Describes extension field in file_managed table.
    $data['file_managed']['extension'] = array(
      'title' => t('Extension'),
      'help' => t('The extension of the file.'),
@@ -129,79 +58,14 @@ public function getViewsData() {
       ),
    );

    // Describes filesize field in file_managed table.
    $data['file_managed']['filesize'] = array(
      'title' => t('Size'),
      'help' => t('The size of the file.'),
      'field' => array(
        'id' => 'file_size',
       ),
      'sort' => array(
        'id' => 'standard',
      ),
      'filter' => array(
        'id' => 'numeric',
      ),
    );

    // Describes status field in file_managed table.
    $data['file_managed']['status'] = array(
      'title' => t('Status'),
      'help' => t('The status of the file.'),
      'field' => array(
        'id' => 'file_status',
       ),
      'sort' => array(
        'id' => 'standard',
      ),
      'filter' => array(
        'id' => 'file_status',
      ),
    );

    // Describes created field in file_managed table.
    $data['file_managed']['created'] = array(
      'title' => t('Upload date'),
      'help' => t('The date the file was uploaded.'),
      'field' => array(
        'id' => 'date',
      ),
      'sort' => array(
        'id' => 'date',
      ),
      'filter' => array(
        'id' => 'date',
      ),
    );
    $data['file_managed']['filesize']['field']['id'] = 'file_size';

    // Describes changed field in file_managed table.
    $data['file_managed']['changed'] = array(
      'title' => t('Modified date'),
      'help' => t('The date the file was last changed.'),
      'field' => array(
        'id' => 'date',
      ),
      'sort' => array(
        'id' => 'date',
      ),
      'filter' => array(
        'id' => 'date',
      ),
    );
    $data['file_managed']['status']['field']['id'] = 'file_status';
    $data['file_managed']['status']['filter']['id'] = 'file_status';

    // Describes uid field in file_managed table.
    $data['file_managed']['uid'] = array(
      'title' => t('User who uploaded'),
      'help' => t('The user that uploaded the file.'),
      'relationship' => array(
        'title' => t('User who uploaded'),
        'label' => t('User who uploaded'),
        'base' => 'users',
        'base field' => 'uid',
      ),
    );
    $data['file_managed']['uid']['relationship']['title'] = t('User who uploaded');
    $data['file_managed']['uid']['relationship']['label'] = t('User who uploaded');

    // Sets 'group' index for file_usage table.
    $data['file_usage']['table']['group']  = t('File Usage');

    // Provide field-type-things to several base tables; on the core files table
@@ -499,4 +363,3 @@ public function getViewsData() {
  }

}
+79 −378
Original line number Diff line number Diff line
@@ -7,169 +7,48 @@

namespace Drupal\node;

use Drupal\views\EntityViewsData;
use Drupal\views\EntityViewsDataInterface;

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

  /**
   * {@inheritdoc}
   */
  public function getViewsData() {
    // Define the base group of this table. Fields that don't have a group defined
    // will go into this field by default.
    $data['node']['table']['group'] = t('Content');
    $data = parent::getViewsData();

    // Advertise this table as a possible base table.
    $data['node']['table']['base'] = array(
      'field' => 'nid',
      'title' => t('Content'),
      'weight' => -10,
      'access query tag' => 'node_access',
      'defaults' => array(
        'field' => 'title',
      ),
    );
    $data['node']['table']['entity type'] = 'node';
    $data['node']['table']['base']['weight'] = -10;
    $data['node']['table']['base']['access query tag'] = 'node_access';
    $data['node']['table']['wizard_id'] = 'node';

    $data['node_field_data']['table']['group'] = t('Content');
    $data['node_field_data']['table']['entity type'] = 'node';
    $data['node_field_data']['table']['join']['node'] = array(
      'type' => 'INNER',
      'left_field' => 'nid',
      'field' => 'nid',
    );

    $data['node']['nid'] = array(
      'title' => t('Nid'),
      'help' => t('The node ID.'),
      'field' => array(
        'id' => 'node',
      ),
      'argument' => array(
    $data['node']['nid']['field']['id'] = 'node';
    $data['node']['nid']['field']['argument'] = [
      'id' => 'node_nid',
      'name field' => 'title',
      'numeric' => TRUE,
      'validate type' => 'nid',
      ),
      'filter' => array(
        'id' => 'numeric',
      ),
      'sort' => array(
        'id' => 'standard',
      ),
    );
    ];

    // This definition has more items in it than it needs to as an example.
    $data['node_field_data']['title'] = array(
      'title' => t('Title'),
      'help' => t('The content title.'),
      'field' => array(
        // This is the real field which could be left out since it is the same.
        'field' => 'title',
        // This is the UI group which could be left out since it is the same.
        'group' => t('Content'),
        'id' => 'node',
        'link_to_node default' => TRUE,
      ),
      'sort' => array(
        'id' => 'standard',
      ),
      'filter' => array(
        'id' => 'string',
      ),
      'argument' => array(
        'id' => 'string',
      ),
    );
    $data['node_field_data']['title']['field']['id'] = 'node';
    $data['node_field_data']['title']['field']['link_to_node default'] = TRUE;

    $data['node_field_data']['created'] = array(
      'title' => t('Post date'),
      'help' => t('The date the content was posted.'),
      'field' => array(
        'id' => 'date',
      ),
      'sort' => array(
        'id' => 'date'
      ),
      'filter' => array(
        'id' => 'date',
      ),
    );
    $data['node_field_data']['type']['field']['id'] = 'node_type';
    $data['node_field_data']['type']['argument']['id'] = 'node_type';

    $data['node_field_data']['changed'] = array(
      'title' => t('Updated date'),
      'help' => t('The date the content was last updated.'),
      'field' => array(
        'id' => 'date',
      ),
      'sort' => array(
        'id' => 'date'
      ),
      'filter' => array(
        'id' => 'date',
      ),
    );
    $data['node_field_data']['langcode']['help'] = t('The language of the content or translation.');
    $data['node_field_data']['langcode']['field']['id'] = 'node_language';

    $data['node_field_data']['type'] = array(
      'title' => t('Type'),
      'help' => t('The content type (for example, "blog entry", "forum post", "story", etc).'),
      'field' => array(
        'id' => 'node_type',
      ),
      'sort' => array(
        'id' => 'standard',
      ),
      'filter' => array(
        'id' => 'bundle',
      ),
      'argument' => array(
        'id' => 'node_type',
      ),
    );

    if (\Drupal::moduleHandler()->moduleExists('language')) {
      $data['node_field_data']['langcode'] = array(
        'title' => t('Translation language'),
        'help' => t('The language of the content or translation.'),
        'field' => array(
          'id' => 'node_language',
        ),
        'filter' => array(
          'id' => 'language',
        ),
        'argument' => array(
          'id' => 'language',
        ),
        'sort' => array(
          'id' => 'standard',
        ),
      );
    }

    $data['node_field_data']['status'] = array(
      'title' => t('Published status'),
      'help' => t('Whether or not the content is published.'),
      'field' => array(
        'id' => 'boolean',
        'output formats' => array(
    $data['node_field_data']['status']['field']['output formats'] = [
      'published-notpublished' => array(t('Published'), t('Not published')),
        ),
      ),
      'filter' => array(
        'id' => 'boolean',
        'label' => t('Published status'),
        'type' => 'yes-no',
    ];
    $data['node_field_data']['status']['filter']['label'] = t('Published status');
    $data['node_field_data']['status']['filter']['type'] = 'yes-no';
    // Use status = 1 instead of status <> 0 in WHERE statement.
        'use_equal' => TRUE,
      ),
      'sort' => array(
        'id' => 'standard',
      ),
    );
    $data['node_field_data']['status']['filter']['use_equal'] = TRUE;

    $data['node_field_data']['status_extra'] = array(
      'title' => t('Published status or admin user'),
@@ -181,44 +60,18 @@ public function getViewsData() {
      ),
    );

    $data['node_field_data']['promote'] = array(
      'title' => t('Promoted to front page status'),
      'help' => t('Whether or not the content is promoted to the front page.'),
      'field' => array(
        'id' => 'boolean',
        'output formats' => array(
    $data['node_field_data']['promote']['field']['output formats'] = [
      'promoted-notpromoted' => array(t('Promoted'), t('Not promoted')),
        ),
      ),
      'filter' => array(
        'id' => 'boolean',
        'label' => t('Promoted to front page status'),
        'type' => 'yes-no',
      ),
      'sort' => array(
        'id' => 'standard',
      ),
    );
    ];
    $data['node_field_data']['promote']['filter']['label'] = t('Promoted to front page status');
    $data['node_field_data']['promote']['filter']['type'] = 'yes-no';

    $data['node_field_data']['sticky'] = array(
      'title' => t('Sticky status'),
      'help' => t('Whether or not the content is sticky.'),
      'field' => array(
        'id' => 'boolean',
        'output formats' => array(
    $data['node_field_data']['sticky']['field']['output formats'] = [
      'sticky' => array(t('Sticky'), t('Not sticky')),
        ),
      ),
      'filter' => array(
        'id' => 'boolean',
        'label' => t('Sticky status'),
        'type' => 'yes-no',
      ),
      'sort' => array(
        'id' => 'standard',
        'help' => t('Whether or not the content is sticky. To list sticky content first, set this to descending.'),
      ),
    );
    ];
    $data['node_field_data']['sticky']['filter']['label'] = t('Sticky status');
    $data['node_field_data']['sticky']['filter']['type'] = 'yes-no';
    $data['node_field_data']['sticky']['sort']['help'] = t('Whether or not the content is sticky. To list sticky content first, set this to descending.');

    if (\Drupal::moduleHandler()->moduleExists('content_translation')) {
      $data['node']['translation_link'] = array(
@@ -264,6 +117,8 @@ public function getViewsData() {

    // Bogus fields for aliasing purposes.

    // @todo Add similar support to any date field
    // @see https://drupal.org/node/2337507
    $data['node_field_data']['created_fulldate'] = array(
      'title' => t('Created date'),
      'help' => t('Date in the form of CCYYMMDD.'),
@@ -372,27 +227,12 @@ public function getViewsData() {
      ),
    );

    $data['node_field_data']['uid'] = array(
      'title' => t('Author uid'),
      'help' => t('The user authoring the content. If you need more fields than the uid add the content: author relationship'),
      'relationship' => array(
        'title' => t('Content author'),
        'help' => t('Relate content to the user who created it.'),
        'id' => 'standard',
        'base' => 'users',
        'field' => 'uid',
        'label' => t('author'),
      ),
      'filter' => array(
        'id' => 'user_name',
      ),
      'argument' => array(
        'id' => 'numeric',
      ),
      'field' => array(
        'id' => 'user',
      ),
    );
    $data['node_field_data']['uid']['help'] = t('The user authoring the content. If you need more fields than the uid add the content: author relationship');
    $data['node_field_data']['uid']['filter']['id'] = 'user_name';
    $data['node_field_data']['uid']['field']['id'] = 'user';
    $data['node_field_data']['uid']['relationship']['title'] = t('Content author');
    $data['node_field_data']['uid']['relationship']['help'] = t('Relate content to the user who created it.');
    $data['node_field_data']['uid']['relationship']['label'] = t('author');

    $data['node']['node_listing_empty'] = array(
      'title' => t('Empty Node Frontpage behavior'),
@@ -402,83 +242,36 @@ public function getViewsData() {
      ),
    );

    $data['node_field_data']['uid_revision'] = array(
      'title' => t('User has a revision'),
      'help' => t('All nodes where a certain user has a revision'),
      'real field' => 'nid',
      'filter' => array(
        'id' => 'node_uid_revision',
      ),
      'argument' => array(
        'id' => 'node_uid_revision',
      ),
    );
    $data['node_field_data']['uid_revision']['title'] = t('User has a revision');
    $data['node_field_data']['uid_revision']['help'] = t('All nodes where a certain user has a revision');
    $data['node_field_data']['uid_revision']['real field'] = 'nid';
    $data['node_field_data']['uid_revision']['filter']['id'] = 'node_uid_revision';
    $data['node_field_data']['uid_revision']['argument']['id'] = 'node_uid_revision';

    $data['node_revision']['table']['entity type'] = 'node';
    // Define the base group of this table. Fields that don't have a group defined
    // will go into this field by default.
    $data['node_revision']['table']['group']  = t('Content revision');
    $data['node_revision']['table']['wizard_id'] = 'node_revision';

    // Advertise this table as a possible base table.
    $data['node_revision']['table']['base'] = array(
      'field' => 'vid',
      'title' => t('Content revision'),
      'help' => t('Content revision is a history of changes to content.'),
      'defaults' => array(
        'field' => 'title',
      ),
    );
    $data['node_revision']['table']['base']['help'] = t('Content revision is a history of changes to content.');
    $data['node_revision']['table']['base']['defaults']['title'] = 'title';

    // For other base tables, explain how we join.
    $data['node_revision']['table']['join'] = array(
      'node' => array(
        'left_field' => 'vid',
        'field' => 'vid',
      ),
    );

    $data['node_revision']['nid'] = array(
      'title' => t('Nid'),
      'help' => t('The revision NID of the content revision.'),
      'field' => array(
        'id' => 'standard',
      ),
      'argument' => array(
    $data['node_revision']['nid']['argument'] = [
      'id' => 'node_nid',
      'numeric' => TRUE,
      ),
      'filter' => array(
        'id' => 'numeric',
      ),
      'sort' => array(
        'id' => 'standard',
      ),
      'relationship' => array(
        'id' => 'standard',
        'base' => 'node',
        'base field' => 'nid',
        'title' => t('Content'),
        'label' => t('Get the actual content from a content revision.'),
      ),
    );
    ];
    // @todo the NID field needs different behaviour on revision/non-revision
    //   tables. It would be neat if this could be encoded in the base field
    //   definition.
    $data['node_revision']['nid']['relationship']['id'] = 'standard';
    $data['node_revision']['nid']['relationship']['base'] = 'node';
    $data['node_revision']['nid']['relationship']['base field'] = 'nid';
    $data['node_revision']['nid']['relationship']['title'] = t('Content');
    $data['node_revision']['nid']['relationship']['label'] = t('Get the actual content from a content revision.');

    $data['node_revision']['vid'] = array(
      'title' => t('Vid'),
      'help' => t('The revision ID of the content revision.'),
      'field' => array(
        'id' => 'standard',
      ),
      'argument' => array(
        'id' => 'node_vid',
        'numeric' => TRUE,
      ),
      'filter' => array(
        'id' => 'numeric',
      ),
      'sort' => array(
        'id' => 'standard',
      ),
      'relationship' => array(
        'id' => 'standard',
        'base' => 'node',
@@ -486,119 +279,29 @@ public function getViewsData() {
        'title' => t('Content'),
        'label' => t('Get the actual content from a content revision.'),
      ),
    );
    ) + $data['node_revision']['vid'];

    if (\Drupal::moduleHandler()->moduleExists('language')) {
      $data['node_revision']['langcode'] = array(
        'title' => t('Original language'),
        'help' => t('The language the original content is in.'),
        'field' => array(
          'id' => 'node_language',
        ),
        'filter' => array(
          'id' => 'language',
        ),
        'argument' => array(
          'id' => 'language',
        ),
        'sort' => array(
          'id' => 'standard',
        ),
      );
    }
    $data['node_revision']['langcode']['help'] = t('The language the original content is in.');
    $data['node_revision']['langcode']['field']['id'] = 'node_language';

    $data['node_revision']['revision_log'] = array(
      'title' => t('Log message'),
      'help' => t('The log message entered when the revision was created.'),
      'field' => array(
        'id' => 'xss',
      ),
      'filter' => array(
        'id' => 'string',
      ),
    );
    $data['node_revision']['revision_log']['field']['id'] = 'xss';

    $data['node_revision']['revision_uid'] = array(
      'title' => t('User'),
      'help' => t('Relate a content revision to the user who created the revision.'),
      'relationship' => array(
        'id' => 'standard',
        'base' => 'users',
        'base field' => 'uid',
        'label' => t('revision user'),
      ),
    );
    $data['node_revision']['revision_uid']['help'] = t('Relate a content revision to the user who created the revision.');
    $data['node_revision']['revision_uid']['relationship']['label'] = t('revision user');

    $data['node_field_revision']['table']['entity type'] = 'node';
    // Define the base group of this table. Fields that don't have a group defined
    // will go into this field by default.
    $data['node_field_revision']['table']['group']  = t('Content revision');
    $data['node_field_revision']['table']['wizard_id'] = 'node_field_revision';

    // For other base tables, explain how we join.
    $data['node_field_revision']['table']['join'] = array(
      'node' => array(
        'left_field' => 'vid',
        'field' => 'vid',
      ),
      'node_revision' => array(
        'left_field' => 'vid',
        'field' => 'vid',
      ),
    );
    $data['node_field_revision']['table']['join']['node']['left_field'] = 'vid';
    $data['node_field_revision']['table']['join']['node']['field'] = 'vid';

    $data['node_field_revision']['status'] = array(
      'title' => t('Published'),
      'help' => t('Whether or not the content is published.'),
      'field' => array(
        'id' => 'boolean',
        'output formats' => array(
          'published-notpublished' => array(t('Published'), t('Not published')),
        ),
      ),
      'filter' => array(
        'id' => 'boolean',
        'label' => t('Published'),
        'type' => 'yes-no',
        // Use status = 1 instead of status <> 0 in WHERE statement.
        'use_equal' => TRUE,
      ),
      'sort' => array(
        'id' => 'standard',
      ),
    );
    $data['node_field_revision']['status']['field']['output formats'] = [
      'published-notpublished' => [t('Published'), t('Not published')],
    ];
    $data['node_field_revision']['status']['filter']['label'] = t('Published');
    $data['node_field_revision']['status']['filter']['type'] = 'yes-no';
    $data['node_field_revision']['status']['filter']['use_equal'] = TRUE;

    $data['node_field_revision']['title'] = array(
      'title' => t('Title'),
      'help' => t('The content title.'),
      'field' => array(
        'field' => 'title',
        'id' => 'node_revision',
      ),
      'sort' => array(
        'id' => 'standard',
      ),
      'filter' => array(
        'id' => 'string',
      ),
      'argument' => array(
        'id' => 'string',
      ),
    );

    $data['node_field_revision']['changed'] = array(
      'title' => t('Updated date'),
      'help' => t('The date the content was last updated.'),
      'field' => array(
        'id' => 'date',
      ),
      'sort' => array(
        'id' => 'date'
      ),
      'filter' => array(
        'id' => 'date',
      ),
    );
    $data['node_field_revision']['title']['field']['id'] = 'node_revision';

    $data['node_revision']['link_to_revision'] = array(
      'field' => array(
@@ -727,6 +430,4 @@ public function getViewsData() {
    return $data;
  }


}
+2 −1
Original line number Diff line number Diff line
@@ -310,7 +310,8 @@
 *     config entities will use \Drupal\Core\Config\Entity\ConfigEntityStorage.
 *     You can extend one of these classes to provide custom behavior.
 *   - views_data: A class implementing \Drupal\views\EntityViewsDataInterface
 *     to provide views data for the entity type.
 *     to provide views data for the entity type. You can autogenerate most of
 *     the views data by extending \Drupal\views\EntityViewsData.
 * - For content entities, the annotation will refer to a number of database
 *   tables and their fields. These annotation properties, such as 'base_table',
 *   'data_table', 'entity_keys', etc., are documented on
+2 −1

File changed.

Preview size limit exceeded, changes collapsed.

Loading