You are here

protected function LightGallery::confGetFieldSources in Lightgallery 8

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

2 calls to LightGallery::confGetFieldSources()
LightGallery::buildOptionsForm in src/Plugin/views/style/LightGallery.php
Provide a form to edit options for this plugin.
LightGallery::renderFields in src/Plugin/views/style/LightGallery.php
Render fields.

File

src/Plugin/views/style/LightGallery.php, line 185

Class

LightGallery
Style plugin to render each item in an ordered or unordered list.

Namespace

Drupal\lightgallery\Plugin\views\style

Code

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

  /** @var \Drupal\views\Plugin\views\field\FieldHandlerInterface $handler */

  // 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 $field => $handler) {
    $is_image = FALSE;
    $id = $handler
      ->getPluginId();
    $name = $field_labels[$field];
    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_type = $handler
        ->getEntityType();

      // Fetch the real field name, because views alters the field name if the
      // same fields gets added multiple times.
      $field_name = $handler->field;
      $field_definition = $this->entityFieldManager
        ->getFieldStorageDefinitions($entity_type)[$field_name];
      if ($field_definition) {
        $field_type = $field_definition
          ->getType();
        if ($field_type == 'image' || $field_type == 'file') {
          $field_cardinality = $field_definition
            ->get('cardinality');
          $options['field_options_images'][$field] = $name . ($field_cardinality == 1 ? '' : '*');
          $is_image = TRUE;
        }
      }
    }
    if (!$is_image) {
      $options['field_options'][$field] = $name;
    }
  }
  return $options;
}