You are here

ui_patterns_settings.module in UI Patterns Settings 8

Same filename and directory in other branches
  1. 8.2 ui_patterns_settings.module

File

ui_patterns_settings.module
View source
<?php

/**
 * @file
 * Contains ui_patterns_settings.module.
 */
use Drupal\ui_patterns_settings\Plugin\Layout\PatternSettingsLayout;
use Drupal\ui_patterns_settings\UiPatternsSettings;
use Drupal\ui_patterns\UiPatterns;
use Drupal\ui_patterns\Definition\PatternDefinition;
use Drupal\ui_patterns_settings\Form\SettingsFormBuilder;

/**
 * Implements hook_ui_patterns_layouts_display_settings_form_alter().
 */
function ui_patterns_settings_ui_patterns_layouts_display_settings_form_alter(&$form, PatternDefinition $definition, $configuration) {
  SettingsFormBuilder::layoutForm($form, $definition, $configuration);
}

/**
 * Implements hook_ui_patterns_display_form_alter().
 */
function ui_patterns_settings_ui_patterns_display_settings_form_alter(array &$form, $configuration) {
  SettingsFormBuilder::displayForm($form, $configuration);
}

/**
 * Preprocess hook.
 *
 * @param array $variables
 *   Theme variables.
 */
function ui_patterns_settings_preprocess_field__pattern_ds_field_template(array &$variables) {
  $pattern_id = $variables['ds-config']['settings']['pattern'];
  $settings = isset($variables['ds-config']['settings']['pattern_settings'][$pattern_id]) ? $variables['ds-config']['settings']['pattern_settings'][$pattern_id] : NULL;
  foreach ($variables['items'] as $delta => $item) {
    $variables['pattern']['pattern_' . $delta]['#settings'] = $settings;
  }
}

/**
 * Preprocess variables for the pattern_views_row theme hook.
 *
 * @param array $variables
 *   Theme variables.
 */
function ui_patterns_settings_preprocess_pattern_views_row(array &$variables) {
  $pattern_id = $variables['options']['pattern'];
  $settings = isset($variables['options']['pattern_settings'][$pattern_id]) ? $variables['options']['pattern_settings'][$pattern_id] : NULL;
  $variables['pattern']['#settings'] = $settings;
}

/**
 * Implements hook_element_info_alter().
 */
function ui_patterns_settings_element_info_alter(array &$info) {
  if (isset($info['pattern'])) {
    $info['pattern']['#pre_render'][] = [
      "Drupal\\ui_patterns_settings\\Element\\PatternSettings",
      "processSettings",
    ];
  }
  if (isset($info['pattern_preview'])) {
    $info['pattern_preview']['#pre_render'][] = [
      "Drupal\\ui_patterns_settings\\Element\\PatternSettings",
      "processPreviewSettings",
    ];
  }
}

/**
 * Implements hook_theme_registry_alter().
 *
 * Add settings variables to the pattern theme.
 */
function ui_patterns_settings_theme_registry_alter(&$theme_registry) {
  foreach (UiPatterns::getManager()
    ->getPatterns() as $pattern) {
    $definition = $pattern
      ->getPluginDefinition();
    if (isset($theme_registry['pattern_' . $definition
      ->id()])) {
      $settings = UiPatternsSettings::getPatternDefinitionSettings($definition);
      foreach ($settings as $key => $setting) {
        $theme_registry['pattern_' . $definition
          ->id()]['variables'][$key] = NULL;
      }
    }
  }
}

/**
 * Implements hook_field_group_build_pre_render_alter().
 */
function ui_patterns_settings_field_group_pre_render_alter(&$element, $group, $rendering_object) {
  $pattern_id = isset($group->format_settings['pattern']) ? $group->format_settings['pattern'] : NULL;
  $element['#settings'] = isset($group->format_settings['pattern_settings'][$pattern_id]) ? $group->format_settings['pattern_settings'][$pattern_id] : NULL;
}

/**
 * Implements hook_layout_alter().
 */
function ui_patterns_settings_layout_alter(&$definitions) {

  /** @var \Drupal\Core\Layout\LayoutDefinition $definition */
  foreach ($definitions as $definition) {
    if ($definition
      ->getClass() == '\\Drupal\\ui_patterns_layouts\\Plugin\\Layout\\PatternLayout') {
      $definition
        ->setClass(PatternSettingsLayout::class);
    }
  }
}