You are here

masked_input.module in Masked Input 8

Same filename and directory in other branches
  1. 7.2 masked_input.module

File

masked_input.module
View source
<?php

/**
 * @file
 * Contains masked_input.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Render\HtmlResponseAttachmentsProcessor;
function masked_input_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'masked_input.settings':
      return '<p>' . t('Masked Input Settings.') . '</p>';
  }
}

/**
 * Implements hook_theme().
 */
function masked_input_theme() {
  $theme = [];
  return $theme;
}

/**
 * Retrieve configured definitions prepared for the #definitions property of
 * the masked_input element type.
 */
function masked_input_get_configured_definitions() {
  $definitions = array();
  $masked_input_definitions = \Drupal::config('masked_input.settings')
    ->get('masked_input_definitions');
  foreach ($masked_input_definitions as $definition) {
    $definitions[$definition['character']] = $definition['regex'];
  }
  return $definitions;
}

/**
 * Returns a build to render a preview of available mask definitions as a table.
 */
function masked_input_view_configured_definitions() {

  // Get default masks.
  $rows = array();

  // Get configured masks.
  $masked_input_definitions = \Drupal::config('masked_input.settings')
    ->get('masked_input_definitions');
  foreach ($masked_input_definitions as $definition) {
    $rows[] = array(
      array(
        'data' => $definition['character'],
      ),
      array(
        'data' => $definition['regex'],
      ),
      array(
        'data' => $definition['description'],
      ),
    );
  }
  return $rows;
}

/**
 * Default mask definitions provided by the plugin. Prepared specifically for
 * 'rows' variable of theme_table().
 */
function _masked_input_default_definitions() {
  return array(
    array(
      array(
        'data' => 'a',
      ),
      array(
        'data' => '[a-zA-Z]',
      ),
      array(
        'data' => 'Represents an alpha character',
      ),
    ),
    array(
      array(
        'data' => '9',
      ),
      array(
        'data' => '[0-9]',
      ),
      array(
        'data' => 'Represents a numeric character',
      ),
    ),
    array(
      array(
        'data' => '*',
      ),
      array(
        'data' => '[A-Za-z0-9]',
      ),
      array(
        'data' => 'Represents an alpha character',
      ),
    ),
  );
}

/**
 * Implements hook_install().
 */
function masked_input_element_info_alter(array &$info) {
  $types['masked_input'] = array(
    '#input' => TRUE,
    '#size' => 60,
    '#maxlength' => 128,
    '#autocomplete_path' => FALSE,
    '#process' => array(
      'masked_input_process_callback',
      'ajax_process_form',
    ),
    '#theme' => 'textfield',
    '#theme_wrappers' => array(
      'form_element',
    ),
    '#mask' => '',
    '#placeholder' => '_',
    '#definitions' => array(),
  );
  return $types;
}

/**
 * Implements hook_install().
 */
function masked_input_install() {

  // Set default values for config which require dynamic values.
  $default_definitions = _masked_input_default_definitions();
  $definitions = array();
  foreach ($default_definitions as $i => $rows) {
    $definitions[$i]['character'] = $rows['0']['data'];
    $definitions[$i]['regex'] = $rows['1']['data'];
    $definitions[$i]['description'] = $rows['2']['data'];
  }
  \Drupal::service('config.factory')
    ->getEditable('masked_input.settings')
    ->set('masked_input_definitions', $definitions)
    ->save();
}

Functions

Namesort descending Description
masked_input_element_info_alter Implements hook_install().
masked_input_get_configured_definitions Retrieve configured definitions prepared for the #definitions property of the masked_input element type.
masked_input_help
masked_input_install Implements hook_install().
masked_input_theme Implements hook_theme().
masked_input_view_configured_definitions Returns a build to render a preview of available mask definitions as a table.
_masked_input_default_definitions Default mask definitions provided by the plugin. Prepared specifically for 'rows' variable of theme_table().