You are here

public function BlazyStylePluginTrait::getDefinedFieldOptions in Blazy 8

Returns available fields for select options.

1 call to BlazyStylePluginTrait::getDefinedFieldOptions()
BlazyViewsTest::buildOptionsForm in tests/modules/blazy_test/src/Plugin/views/style/BlazyViewsTest.php
Overrides parent::buildOptionsForm().

File

src/Dejavu/BlazyStylePluginTrait.php, line 16

Class

BlazyStylePluginTrait
A Trait common for optional views style plugins.

Namespace

Drupal\blazy\Dejavu

Code

public function getDefinedFieldOptions($definitions = []) {
  $field_names = $this->displayHandler
    ->getFieldLabels();
  $definition = [];
  $stages = [
    'blazy_media',
    'block_field',
    'colorbox',
    'entity_reference_entity_view',
    'gridstack_file',
    'gridstack_media',
    'photobox',
    'video_embed_field_video',
    'youtube_video',
  ];

  // Formatter based fields.
  $options = [];
  foreach ($this->displayHandler
    ->getOption('fields') as $field => $handler) {

    // This is formatter based type, not actual field type.
    if (isset($handler['type'])) {
      switch ($handler['type']) {

        // @todo recheck other reasonable image-related formatters.
        case 'blazy':
        case 'image':
        case 'media':
        case 'media_thumbnail':
        case 'intense':
        case 'responsive_image':
        case 'video_embed_field_thumbnail':
        case 'video_embed_field_colorbox':
        case 'youtube_thumbnail':
          $options['images'][$field] = $field_names[$field];
          $options['overlays'][$field] = $field_names[$field];
          $options['thumbnails'][$field] = $field_names[$field];
          break;
        case 'list_key':
          $options['layouts'][$field] = $field_names[$field];
          break;
        case 'entity_reference_label':
        case 'text':
        case 'string':
        case 'link':
          $options['links'][$field] = $field_names[$field];
          $options['titles'][$field] = $field_names[$field];
          if ($handler['type'] != 'link') {
            $options['thumb_captions'][$field] = $field_names[$field];
          }
          break;
      }
      $classes = [
        'list_key',
        'entity_reference_label',
        'text',
        'string',
      ];
      if (in_array($handler['type'], $classes)) {
        $options['classes'][$field] = $field_names[$field];
      }
      $slicks = strpos($handler['type'], 'slick') !== FALSE;
      if ($slicks || in_array($handler['type'], $stages)) {
        $options['overlays'][$field] = $field_names[$field];
      }

      // Allows advanced formatters/video as the main image replacement.
      // They are not reasonable for thumbnails, but main images.
      // Note: Certain Responsive image has no ID at Views, possibly a bug.
      if (in_array($handler['type'], $stages)) {
        $options['images'][$field] = $field_names[$field];
      }
    }

    // Content: title is not really a field, unless title.module installed.
    if (isset($handler['field'])) {
      if ($handler['field'] == 'title') {
        $options['classes'][$field] = $field_names[$field];
        $options['titles'][$field] = $field_names[$field];
        $options['thumb_captions'][$field] = $field_names[$field];
      }
      if (in_array($handler['field'], [
        'nid',
        'nothing',
        'view_node',
      ])) {
        $options['links'][$field] = $field_names[$field];
        $options['titles'][$field] = $field_names[$field];
      }
      $blazies = strpos($handler['field'], 'blazy_') !== FALSE;
      if ($blazies) {
        $options['images'][$field] = $field_names[$field];
        $options['overlays'][$field] = $field_names[$field];
        $options['thumbnails'][$field] = $field_names[$field];
      }
    }

    // Captions can be anything to get custom works going.
    $options['captions'][$field] = $field_names[$field];
  }
  $definition['plugin_id'] = $this
    ->getPluginId();
  $definition['settings'] = $this->options;
  $definition['current_view_mode'] = $this->view->current_display;

  // Provides the requested fields.
  foreach ($definitions as $key) {
    $definition[$key] = isset($options[$key]) ? $options[$key] : [];
  }
  $contexts = [
    'handler' => $this->displayHandler,
    'view' => $this->view,
  ];
  $this->blazyManager
    ->getModuleHandler()
    ->alter('blazy_views_field_options', $definition, $contexts);
  return $definition;
}