You are here

function scheduler_views_data_alter in Scheduler 2.x

Same name and namespace in other branches
  1. 8 scheduler.module \scheduler_views_data_alter()

Implements hook_views_data_alter().

File

./scheduler.module, line 641
Scheduler publishes and unpublishes entities on dates specified by the user.

Code

function scheduler_views_data_alter(array &$data) {

  // By default the 'is null' and 'is not null' operators are only added to the
  // list of filter options if the view contains a relationship. We want them to
  // be always available for the scheduler date fields.
  $entity_types = \Drupal::service('scheduler.manager')
    ->getPluginEntityTypes();
  foreach ($entity_types as $entityTypeId) {

    // Not every entity that has a plugin will have these tables, so only set
    // the allow_empty filter if the top-level key exists.
    if (isset($data["{$entityTypeId}_field_data"])) {
      $data["{$entityTypeId}_field_data"]['publish_on']['filter']['allow empty'] = TRUE;
      $data["{$entityTypeId}_field_data"]['unpublish_on']['filter']['allow empty'] = TRUE;
    }
    if (isset($data["{$entityTypeId}_field_revision"])) {
      $data["{$entityTypeId}_field_revision"]['publish_on']['filter']['allow empty'] = TRUE;
      $data["{$entityTypeId}_field_revision"]['unpublish_on']['filter']['allow empty'] = TRUE;
    }
  }

  // Add a relationship from Media Field Revision back to Media Field Data.
  // @todo This can be removed when the relationship is added to core.
  // @see https://www.drupal.org/project/drupal/issues/3036192
  // Replace the existing 'argument' item.
  $data['media_field_revision']['mid']['argument'] = [
    'id' => 'media_mid',
    'numeric' => TRUE,
  ];

  // Add a 'relationship' item.
  $data['media_field_revision']['mid']['relationship'] = [
    'id' => 'standard',
    'base' => 'media_field_data',
    'field' => 'mid',
    'base field' => 'mid',
    'title' => t('Media Field Data'),
    'help' => t('Relationship to access the Media fields that are not on Media Revision.'),
    'label' => t('Media Field'),
    'extra' => [
      [
        'field' => 'langcode',
        'left_field' => 'langcode',
      ],
    ],
  ];
}