You are here

public function JuiceboxDisplayStyle::confGetFieldSources in Juicebox HTML5 Responsive Image Galleries 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/style/JuiceboxDisplayStyle.php \Drupal\juicebox\Plugin\views\style\JuiceboxDisplayStyle::confGetFieldSources()

Utility to determine which view fields can be used for image data.

This method will extract a list of fields that can be used as "sources" for a Juicebox gallery along with other useful field information.

Return value

array An associative array containing a breakdown of field data that can be referenced by other build methods, including:

  • field_options_image: An associative array, keyed by field id, of fields that can be used as Juicebox gallery image sources.
  • field_options_image_type: An associative array, keyed by field id, of field "types" for all fields listed in 'field_options_image' above.
  • field_options: An associative array, keyed by field id, of fields that cannot be used as Juicebox gallery image sources, but may be useful for other purposes (text and caption sorces, etc.)
2 calls to JuiceboxDisplayStyle::confGetFieldSources()
JuiceboxDisplayStyle::buildOptionsForm in src/Plugin/views/style/JuiceboxDisplayStyle.php
Provide a form to edit options for this plugin.
JuiceboxDisplayStyle::getItems in src/Plugin/views/style/JuiceboxDisplayStyle.php
Utility to get the item arrays that contain image data from view rows.

File

src/Plugin/views/style/JuiceboxDisplayStyle.php, line 429

Class

JuiceboxDisplayStyle
Plugin implementation of the 'juicebox' display style.

Namespace

Drupal\juicebox\Plugin\views\style

Code

public function confGetFieldSources() {
  $options = [
    'field_options_images' => [],
    'field_options_images_type' => [],
    'field_options' => [],
  ];
  $view = $this->view;
  $field_handlers = $view->display_handler
    ->getHandlers('field');
  $field_labels = $view->display_handler
    ->getFieldLabels();

  // Separate image fields from non-image fields. For image fields we can
  // work with fids and fields of type image or file.
  foreach ($field_handlers as $viewfield => $handler) {
    $is_image = FALSE;
    $id = $handler
      ->getPluginId();
    $name = $field_labels[$viewfield];
    if ($id == 'field') {

      // The field definition is on the handler, it's right bloody there, but
      // it's protected so we can't access it. This means we have to take the
      // long road (via our own injected entity manager) to get the field type
      // info.
      $entity = $this->entityFieldManager
        ->getFieldStorageDefinitions($handler
        ->getEntityType());
      if (isset($handler->field) && array_key_exists($handler->field, $entity)) {
        $field_definition = $entity[$handler->field];
        $field_type = $field_definition
          ->getType();
        if ($field_type == 'image' || $field_type == 'file' || $field_type == 'entity_reference') {
          $field_cardinality = $field_definition
            ->get('cardinality');
          $options['field_options_images'][$viewfield] = $name . ($field_cardinality == 1 ? '' : '*');
          $options['field_options_images_type'][$viewfield] = 'file_field';
          $is_image = TRUE;
        }
        elseif ($field_type == 'integer' && $handler->field == 'fid') {
          $options['field_options_images'][$viewfield] = $name;
          $options['field_options_images_type'][$viewfield] = 'file_id_field';
          $is_image = TRUE;
        }
      }
    }
    elseif ($id == 'file' && $viewfield == 'fid') {
      $options['field_options_images'][$viewfield] = $name;
      $options['field_options_images_type'][$viewfield] = 'file_id_field';
      $is_image = TRUE;
    }
    if (!$is_image) {
      $options['field_options'][$viewfield] = $name;
    }
  }
  return $options;
}