You are here

label_length_limit.module in Override label length limititation 2.0.x

Same filename and directory in other branches
  1. 8 label_length_limit.module

File

label_length_limit.module
View source
<?php

/**
 * @file
 * Contains label_length_limit.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function label_length_limit_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the label_length_limit module.
    case 'help.page.label_length_limit':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Remove Label Field Limitation') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function label_length_limit_form_field_config_edit_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['label'])) {
    $config = \Drupal::config('label_length_limit.labellengthlimit');
    $label_max_length = $config
      ->get('label_max_length');
    $form['label']['#maxlength'] = $label_max_length;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function label_length_limit_form_field_ui_field_storage_add_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['new_storage_wrapper']['label'])) {
    $config = \Drupal::config('label_length_limit.labellengthlimit');
    $label_max_length = $config
      ->get('label_max_length');
    $form['new_storage_wrapper']['label']['#maxlength'] = $label_max_length;
  }
  if (isset($form['existing_storage_label'])) {
    $form['existing_storage_label']['#maxlength'] = $label_max_length;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function label_length_limit_form_views_ui_config_item_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['options']['label'])) {
    $config = \Drupal::config('label_length_limit.labellengthlimit');
    $label_max_length = $config
      ->get('label_max_length');
    $form['options']['label']['#maxlength'] = $label_max_length;
  }
}

/**
 * Implements hook_module_implements_alter().
 */
function label_length_limit_module_implements_alter(&$implementations, $hook) {
  if ($hook !== 'form_alter') {
    return;
  }
  if (!isset($implementations['label_length_limit'])) {
    return;
  }
  $implementation = $implementations['label_length_limit'];
  unset($implementations['label_length_limit']);
  $implementations['label_length_limit'] = $implementation;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function label_length_limit_form_config_translation_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  _label_length_limit_set_translation_label_length($form, $form_state, $form_id);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function label_length_limit_form_config_translation_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  _label_length_limit_set_translation_label_length($form, $form_state, $form_id);
}

/**
 * Change max length limit on label fields.
 *
 * @param mixed $form
 *   Nested array of form elements that comprise the form.
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form. The arguments that
 *   \Drupal::formBuilder()->getForm() was originally called with are available
 *   in the array $form_state->getBuildInfo()['args'].
 * @param string $form_id
 *   String representing the name of the form itself. Typically this is the
 *   name of the function that generated the form.
 */
function _label_length_limit_set_translation_label_length(&$form, FormStateInterface $form_state, $form_id) {
  $config = \Drupal::config('label_length_limit.labellengthlimit');
  $label_max_length = $config
    ->get('label_max_length');
  foreach ($form['config_names'] as $config_name => $config_value) {
    if (isset($form['config_names'][$config_name]['label']['translation']) && $form['config_names'][$config_name]['label']['translation']['#type'] === 'textfield') {
      $form['config_names'][$config_name]['label']['translation']['#maxlength'] = $label_max_length;
    }
  }
}