You are here

fz152.module in FZ152 8

Same filename and directory in other branches
  1. 7 fz152.module

Main file for hooks and custom functions.

File

fz152.module
View source
<?php

/**
 * @file
 * Main file for hooks and custom functions.
 */

/**
 * Check is provided form id is match any of patterns.
 */
function fz152_form_id_matches($form_id, $patterns) {

  // Replace new lines by "|" and wildcard by ".*".
  $to_replace = [
    '/(\\r\\n?|\\n)/',
    // newlines
    '/\\\\\\*/',
  ];
  $replacements = [
    '|',
    '.*',
  ];
  $patterns_quoted = preg_quote($patterns, '/');
  $pattern = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
  preg_match($pattern, $form_id, $matches);
  return $matches;
}

/**
 * Implements hook_form_alter().
 */
function fz152_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $config = \Drupal::config('fz152.settings');
  $plugin_service = \Drupal::service('plugin.manager.fz152');
  $active_forms = [];
  foreach ($plugin_service
    ->getDefinitions() as $plugin_id => $plugin) {
    $instance = $plugin_service
      ->createInstance($plugin_id);
    $active_forms = array_merge($active_forms, $instance
      ->getForms());
  }
  if ($config
    ->get('enable')) {
    $pattern_for_forms = '';
    foreach ($active_forms as $current_form) {
      $pattern_for_forms .= $current_form['form_id'] . PHP_EOL;
    }
    $matches = fz152_form_id_matches($form_id, $pattern_for_forms);
    if (!empty($matches)) {
      $matched_form_id = $matches[0];

      // Find checkbox weight by matched form id with available in array.
      // Because we have support for wildcards, this is necessary.
      $checkbox_weight = NULL;
      foreach ($active_forms as $k => $v) {
        $current_form_id = str_replace('*', '.*', $v['form_id']);
        $pattern = "/{$current_form_id}/";
        if (preg_match($pattern, $matched_form_id)) {
          $checkbox_weight = $v['weight'];
          break;
        }
      }

      // Finally we add checkbox.
      $is_checkbox = $config
        ->get('is_checkbox');
      $checkbox_form = [
        '#type' => 'checkbox',
        '#required' => TRUE,
        '#title' => $config
          ->get('checkbox_title'),
        // HTML5 support.
        '#attributes' => [
          'required' => 'required',
        ],
        '#weight' => $checkbox_weight,
        '#element_validate' => [
          'fz152_agreement_element_validate',
        ],
      ];
      $text_form = [
        '#name' => 'fz152-agreement',
        '#type' => 'item',
        '#markup' => $config
          ->get('checkbox_title'),
        '#weight' => $checkbox_weight,
      ];
      if (isset($form['elements']['actions']['#type']) && $form['elements']['actions']['#type'] == 'webform_actions') {
        $new = array();
        foreach ($form['elements'] as $f => $value) {
          if ($f === 'actions') {
            $new['fz152_agreement'] = $is_checkbox ? $checkbox_form : $text_form;
          }
          $new[$f] = $value;
          $form['elements'] = $new;
        }
      }
      else {
        $form['actions']['#weight'] = 110;
        $form['fz152_agreement'] = $is_checkbox ? $checkbox_form : $text_form;
      }
    }
  }
}

/**
 * Implements hook_menu_links_discovered_alter().
 */
function fz152_menu_links_discovered_alter(&$links) {
  $plugin_service = \Drupal::service('plugin.manager.fz152');
  foreach ($plugin_service
    ->getDefinitions() as $plugin_id => $plugin) {
    $instance = $plugin_service
      ->createInstance($plugin_id);
    $route_info = $instance
      ->getSettingsPage();
    $links['fz152.admin.' . $plugin_id] = [
      'title' => $route_info['title'],
      'route_name' => 'fz152.settings.' . $plugin_id,
      'parent' => 'fz152.admin',
    ];
  }
}

/**
 * Custom validation for FZ152 agreement checkbox.
 *
 * The common usage of element is contain HTML in label of element. When user
 * submit the form without checking this element, the default form throws error
 * with HTML in it, so, this is fixes the with error when element is not
 * checked.
 *
 * @param array $element
 *   Form element to be validated.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   Current form state.
 * @param string $form_id
 *   Submitted form id.
 */
function fz152_agreement_element_validate(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if (!$form_state
    ->getValue('fz152_agreement')) {
    $form_state
      ->setError($element, t('You must agree with privacy policy.'));
  }
}

Functions

Namesort descending Description
fz152_agreement_element_validate Custom validation for FZ152 agreement checkbox.
fz152_form_alter Implements hook_form_alter().
fz152_form_id_matches Check is provided form id is match any of patterns.
fz152_menu_links_discovered_alter Implements hook_menu_links_discovered_alter().