You are here

function rest_views_views_data_alter in REST Views 8

Same name and namespace in other branches
  1. 2.0.x rest_views.module \rest_views_views_data_alter()

Implements hook_views_data_alter().

File

./rest_views.module, line 11
Provides a new Views Field plugin for all entity fields.

Code

function rest_views_views_data_alter(array &$data) {
  foreach ($data as $table_alias => $fields) {

    /** @var array[] $fields */
    foreach ($fields as $field_alias => $field) {

      // Find all handlers that use the core Field plugin.
      if (isset($field['field']['id']) && $field['field']['id'] === 'field') {

        // Add a second handler that uses the Rest Field plugin.
        $field['title'] = t('@field (serializable)', [
          '@field' => $field['title'],
        ]);
        $field['field']['id'] = 'field_export';

        // Only expose the field handler as the others are redundant.
        unset($field['argument'], $field['filter'], $field['sort']);
        $data[$table_alias]["{$field_alias}_export"] = $field;
      }
    }
  }
}