You are here

ui_patterns_ds.module in UI Patterns 8

Contains module file.

File

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

/**
 * @file
 * Contains module file.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\ui_patterns\Form\PatternDisplayFormTrait;
use Drupal\ui_patterns\Element\PatternContext;

/**
 * Implements hook_theme().
 */
function ui_patterns_ds_theme() {
  return [
    'pattern_ds_field_template' => [
      'variables' => [
        'pattern' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ui_patterns_ds_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  array_unshift($form['actions']['submit']['#submit'], 'ui_patterns_ds_field_overview_submit');
}

/**
 * Form submit callback to fix the field_group configuration.
 *
 * @param array $form
 *   The form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The form state.
 */
function ui_patterns_ds_field_overview_submit(array $form, FormStateInterface $form_state) {

  /* @var \Drupal\Core\Entity\EntityFormInterface $entity_form */

  /* @var \Drupal\Core\Entity\Display\EntityDisplayInterface $display */
  $entity_form = $form_state
    ->getFormObject();
  $display = $entity_form
    ->getEntity();
  $components = array_filter($display
    ->getComponents(), function ($component) {
    return isset($component['third_party_settings']['ds']['ft']['settings']['pattern']);
  });
  foreach ($components as $name => $component) {
    PatternDisplayFormTrait::processFormStateValues($component['third_party_settings']['ds']['ft']['settings']);
    $display
      ->setComponent($name, $component);
  }
  $display
    ->save();
}

/**
 * Preprocess hook.
 *
 * @param array $variables
 *   Theme variables.
 */
function template_preprocess_field__pattern_ds_field_template(array &$variables) {
  \Drupal::service('ui_patterns_ds.field_template_processor')
    ->process($variables);
}

/**
 * Implements hook_ui_patterns_suggestions_alter().
 */
function ui_patterns_ds_ui_patterns_suggestions_alter(array &$suggestions, array $variables, PatternContext $context) {
  if ($context
    ->isOfType('ds_field_template')) {
    $hook = $variables['theme_hook_original'];
    $variant = isset($variables["variant"]) ? $variables["variant"] : '';
    $field_name = $context
      ->getProperty('field_name');
    $entity_type = $context
      ->getProperty('entity_type');
    $bundle = $context
      ->getProperty('bundle');
    $view_mode = $context
      ->getProperty('view_mode');
    $suggestions[] = $hook . '__ds_field_template';
    $suggestions[] = $hook . '__ds_field_template__' . $field_name;
    $suggestions[] = $hook . '__ds_field_template__' . $field_name . '__' . $entity_type;
    $suggestions[] = $hook . '__ds_field_template__' . $field_name . '__' . $entity_type . '__' . $bundle;
    $suggestions[] = $hook . '__ds_field_template__' . $field_name . '__' . $entity_type . '__' . $view_mode;
    $suggestions[] = $hook . '__ds_field_template__' . $field_name . '__' . $entity_type . '__' . $bundle . '__' . $view_mode;
    if (!empty($variant)) {
      $suggestions[] = $hook . '__variant_' . $variant . '__ds_field_template';
      $suggestions[] = $hook . '__variant_' . $variant . '__ds_field_template__' . $field_name;
      $suggestions[] = $hook . '__variant_' . $variant . '__ds_field_template__' . $field_name . '__' . $entity_type;
      $suggestions[] = $hook . '__variant_' . $variant . '__ds_field_template__' . $field_name . '__' . $entity_type . '__' . $bundle;
      $suggestions[] = $hook . '__variant_' . $variant . '__ds_field_template__' . $field_name . '__' . $entity_type . '__' . $view_mode;
      $suggestions[] = $hook . '__variant_' . $variant . '__ds_field_template__' . $field_name . '__' . $entity_type . '__' . $bundle . '__' . $view_mode;
    }
  }
}