You are here

function commerce_reports_bundle_plugin_fields_views_data in Commerce Reporting 8

Implements hook_field_views_data() views integrations for various fields.

The problem with hook_field_views_data() is that it assumes the field being passed in implements FieldStorageConfigInterface and not a generic FieldStorageDefinitionInterface implementation.

The contents of this method are essentially copied code from the respective hook implementations.

Parameters

\Drupal\entity\BundleFieldDefinition $field_storage: The field storage config entity.

See also

hook_field_views_data()

1 call to commerce_reports_bundle_plugin_fields_views_data()
commerce_reports_views_data_alter in ./commerce_reports.views.inc
Implements hook_views_data_alter().

File

./commerce_reports.views.inc, line 69
Views integration for Commerce reports.

Code

function commerce_reports_bundle_plugin_fields_views_data(array &$data, BundleFieldDefinition $field_storage) {
  switch ($field_storage
    ->getType()) {
    case 'address':

      // @see address_field_views_data().
      $field_name = $field_storage
        ->getName();
      $columns = [
        'country_code' => 'country_code',
        'administrative_area' => 'subdivision',
        'locality' => 'subdivision',
        'dependent_locality' => 'subdivision',
        'postal_code' => 'standard',
        'sorting_code' => 'standard',
        'address_line1' => 'standard',
        'address_line2' => 'standard',
        'organization' => 'standard',
        'given_name' => 'standard',
        'additional_name' => 'standard',
        'family_name' => 'standard',
      ];
      foreach ($data as $table_name => $table_data) {
        foreach ($columns as $column => $plugin_id) {
          $data[$table_name][$field_name . '_' . $column]['field'] = [
            'id' => $plugin_id,
            'field_name' => $field_name,
            'property' => $column,
          ];
        }

        // Add the custom country_code filter.
        $data[$table_name][$field_name . '_country_code']['filter']['id'] = 'country_code';

        // Add the custom administrative_area filter.
        $data[$table_name][$field_name . '_administrative_area']['filter']['id'] = 'administrative_area';
      }
      break;
    case 'address_country':

      // @see address_field_views_data().
      $field_name = $field_storage
        ->getName();
      foreach ($data as $table_name => $table_data) {
        $data[$table_name][$field_name . '_value']['field'] = [
          'id' => 'country_code',
          'field_name' => $field_name,
          'property' => 'value',
        ];
        $data[$table_name][$field_name . '_value']['filter']['id'] = 'country_code';
      }
      break;
    case 'commerce_price':

      // @see commerce_price_field_views_data().
      $field_name = $field_storage
        ->getName();
      foreach ($data as $table_name => $table_data) {
        if (isset($table_data[$field_name])) {
          $data[$table_name][$field_name . '_number']['field'] = [
            'id' => 'numeric',
            'field_name' => $table_data[$field_name]['field']['field_name'],
            'entity_type' => $table_data[$field_name]['field']['entity_type'],
            'label' => t('number from @field_name', [
              '@field_name' => $field_name,
            ]),
          ];
          $data[$table_name][$field_name . '_currency_code']['field'] = [
            'id' => 'standard',
            'field_name' => $table_data[$field_name]['field']['field_name'],
            'entity_type' => $table_data[$field_name]['field']['entity_type'],
            'label' => t('currency from @field_name', [
              '@field_name' => $field_name,
            ]),
          ];
        }
      }
      break;
    case 'datetime':

      // @see datetime_field_views_data().
      foreach ($data as $table_name => $table_data) {

        // Set the 'datetime' filter type.
        $data[$table_name][$field_storage
          ->getName() . '_value']['filter']['id'] = 'datetime';

        // Set the 'datetime' argument type.
        $data[$table_name][$field_storage
          ->getName() . '_value']['argument']['id'] = 'datetime';

        // Create year, month, and day arguments.
        $group = $data[$table_name][$field_storage
          ->getName() . '_value']['group'];
        $arguments = [
          // Argument type => help text.
          'year' => t('Date in the form of YYYY.'),
          'month' => t('Date in the form of MM (01 - 12).'),
          'day' => t('Date in the form of DD (01 - 31).'),
          'week' => t('Date in the form of WW (01 - 53).'),
          'year_month' => t('Date in the form of YYYYMM.'),
          'full_date' => t('Date in the form of CCYYMMDD.'),
        ];
        foreach ($arguments as $argument_type => $help_text) {
          $data[$table_name][$field_storage
            ->getName() . '_value_' . $argument_type] = [
            'title' => $field_storage
              ->getLabel() . ' (' . $argument_type . ')',
            'help' => $help_text,
            'argument' => [
              'field' => $field_storage
                ->getName() . '_value',
              'id' => 'datetime_' . $argument_type,
              'entity_type' => $field_storage
                ->getTargetEntityTypeId(),
              'field_name' => $field_storage
                ->getName(),
            ],
            'group' => $group,
          ];
        }

        // Set the 'datetime' sort handler.
        $data[$table_name][$field_storage
          ->getName() . '_value']['sort']['id'] = 'datetime';
      }
      break;
    case 'list_float':
    case 'list_integer':
    case 'list_string':

      // @see options_field_views_data().
      foreach ($data as $table_name => $table_data) {
        foreach ($table_data as $field_name => $field_data) {
          if (isset($field_data['filter']) && $field_name != 'delta') {
            $data[$table_name][$field_name]['filter']['id'] = 'list_field';
          }
          if (isset($field_data['argument']) && $field_name != 'delta') {
            if ($field_storage
              ->getType() == 'list_string') {
              $data[$table_name][$field_name]['argument']['id'] = 'string_list_field';
            }
            else {
              $data[$table_name][$field_name]['argument']['id'] = 'number_list_field';
            }
          }
        }
      }
      break;
    case 'state':

      // @see state_machine_field_views_data().
      $field_name = $field_storage
        ->getName();
      foreach ($data as $table_name => $table_data) {
        $data[$table_name][$field_name . '_value']['filter']['id'] = 'state_machine_state';
      }
      break;
  }
}