You are here

ui_patterns_views.module in UI Patterns 8

Contains module file.

File

modules/ui_patterns_views/ui_patterns_views.module
View source
<?php

/**
 * @file
 * Contains module file.
 */
use Drupal\ui_patterns\Element\PatternContext;

/**
 * Implements hook_theme().
 */
function ui_patterns_views_theme() {
  return [
    'pattern_views_row' => [
      'variables' => [
        'view' => NULL,
        'options' => [],
        'row' => NULL,
      ],
    ],
  ];
}

/**
 * Preprocess hook.
 *
 * @param array $variables
 *   Theme variables.
 */
function template_preprocess_pattern_views_row(array &$variables) {

  /** @var \Drupal\views\ResultRow $row */

  /** @var \Drupal\views\ViewExecutable $view */

  /** @var \Drupal\ui_patterns_views\Plugin\views\row\Pattern $row_plugin */
  $fields = [];
  $view = $variables['view'];
  $row_plugin = $view->rowPlugin;
  $options = $variables['options'];
  $row = $variables['row'];
  foreach ($options['pattern_mapping'] as $mapping) {
    $field_name = $mapping['source'];
    $field = $view->field[$field_name];
    $field_output = $view->style_plugin
      ->getField($row->index, $field_name);
    if ($row_plugin
      ->isFieldVisible($field, $field_output)) {
      $destination = $row_plugin
        ->getMappingDestination('views_row', $field_name, $options);
      $fields[$destination][] = $field_output;
    }
  }
  $variables['pattern'] = [];
  if ($view->preview && !isset($view->element['#embed'])) {
    \Drupal::messenger()
      ->addWarning(t("Pattern Views row plugin does not support preview."));
    $variables['pattern'] = [
      '#type' => 'status_messages',
    ];
  }
  elseif (!empty($fields)) {
    $variables['pattern'] = [
      '#type' => 'pattern',
      '#id' => $options['pattern'],
      '#fields' => $fields,
      '#multiple_sources' => TRUE,
      '#variant' => isset($options['pattern_variant']) ? $options['pattern_variant'] : '',
    ];

    // Allow default context values to not override those exposed elsewhere.
    $variables['pattern']['#context']['type'] = 'views_row';
    $variables['pattern']['#context']['view_name'] = $view->storage
      ->id();
    $variables['pattern']['#context']['display'] = $view->current_display;
    $variables['pattern']['#context']['view'] = $view;
    $variables['pattern']['#context']['row'] = $row;
  }
}

/**
 * Implements hook_ui_patterns_suggestions_alter().
 */
function ui_patterns_views_ui_patterns_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
  if ($context
    ->isOfType('views_row')) {
    $hook = $variables['theme_hook_original'];
    $variant = isset($variables["variant"]) ? $variables["variant"] : '';
    $view_name = $context
      ->getProperty('view_name');
    $display = $context
      ->getProperty('display');
    $suggestions[] = $hook . '__views_row';
    $suggestions[] = $hook . '__views_row__' . $view_name;
    $suggestions[] = $hook . '__views_row__' . $view_name . '__' . $display;
    if (!empty($variant)) {
      $suggestions[] = $hook . '__variant_' . $variant . '__views_row';
      $suggestions[] = $hook . '__variant_' . $variant . '__views_row__' . $view_name;
      $suggestions[] = $hook . '__variant_' . $variant . '__views_row__' . $view_name . '__' . $display;
    }
  }
}

/**
 * Implements hook_ui_patterns_destination_suggestions_alter().
 */
function ui_patterns_views_ui_patterns_destination_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
  if ($context
    ->isOfType('views_row')) {
    $hook = $variables['theme_hook_original'];
    $variant = isset($variables["variant"]) ? $variables["variant"] : '';
    $view_name = $context
      ->getProperty('view_name');
    $display = $context
      ->getProperty('display');
    $pattern = $context
      ->getProperty('pattern');
    $field = $context
      ->getProperty('field');
    $suggestions[] = $hook . '__views_row__' . $view_name . '__' . $pattern . '__' . $field;
    $suggestions[] = $hook . '__views_row__' . $view_name . '__' . $display . '__' . $pattern . '__' . $field;
    if (!empty($variant)) {
      $suggestions[] = $hook . '__variant_' . $variant . '__views_row__' . $view_name . '__' . $pattern . '__' . $field;
      $suggestions[] = $hook . '__variant_' . $variant . '__views_row__' . $view_name . '__' . $display . '__' . $pattern . '__' . $field;
    }
  }
}