You are here

public function BlazyStyleOptionsTrait::getDefinedFieldOptions in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/Dejavu/BlazyStyleOptionsTrait.php \Drupal\blazy\Dejavu\BlazyStyleOptionsTrait::getDefinedFieldOptions()

Returns available fields for select options.

File

src/Dejavu/BlazyStyleOptionsTrait.php, line 20

Class

BlazyStyleOptionsTrait
A Trait common for optional views style plugins.

Namespace

Drupal\blazy\Dejavu

Code

public function getDefinedFieldOptions($defined_options = []) {
  $fields = $this->view->display_handler
    ->get_handlers('field');
  $options = [];
  $classes = [
    'list_text',
    'entityreference',
    'taxonomy_term_reference',
    'text',
  ];
  foreach ($fields as $field => $handler) {
    if (isset($handler->field_info)) {
      $type = $handler->field_info['type'];
      switch ($type) {
        case 'file':
        case 'image':
        case 'youtube':
        case 'video_embed_field':
          $options['images'][$field] = $handler
            ->ui_name();
          $options['overlays'][$field] = $handler
            ->ui_name();
          $options['thumbnails'][$field] = $handler
            ->ui_name();
          break;
        case 'list_text':
          $options['layouts'][$field] = $handler
            ->ui_name();
          break;
        case 'entityreference':
        case 'text':
        case 'text_long':
        case 'text_with_summary':
        case 'link_field':
          $options['links'][$field] = $handler
            ->ui_name();
          $options['titles'][$field] = $handler
            ->ui_name();
          if ($type != 'link_field') {
            $options['thumb_captions'][$field] = $handler
              ->ui_name();
          }
          break;
      }
      if (in_array($type, $classes)) {
        $options['classes'][$field] = $handler
          ->ui_name();
      }
    }

    // Content: title is not really a field, unless title.module installed.
    if ($handler->field == 'title') {
      $options['classes'][$field] = $handler
        ->ui_name();
      $options['titles'][$field] = $handler
        ->ui_name();
      $options['thumb_captions'][$field] = $handler
        ->ui_name();
    }
    if ($handler->field == 'nothing') {
      $options['classes'][$field] = $handler
        ->ui_name();
    }
    if (in_array($handler->field, [
      'nid',
      'nothing',
      'view_node',
    ])) {
      $options['links'][$field] = $handler
        ->ui_name();
      $options['titles'][$field] = $handler
        ->ui_name();
    }

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

  // Provides the requested fields based on available $options.
  foreach ($defined_options as $key) {
    $definition[$key] = isset($options[$key]) ? $options[$key] : [];
  }
  $contexts = [
    'handler' => $this->view->display_handler,
    'view' => $this->view,
  ];
  drupal_alter('blazy_views_field_options', $definition, $contexts);
  return $definition;
}