You are here

public function TextBaseTrait::buildCounterForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Plugin/WebformElement/TextBaseTrait.php \Drupal\webform\Plugin\WebformElement\TextBaseTrait::buildCounterForm()

Build counter widget used by text elements and other element.

Parameters

string $name: Property name prefix.

string $title: Property title prefix.

Return value

array A renderable array containing a counter configuration form.

See also

\Drupal\webform\Plugin\WebformElement\TextBase::form

\Drupal\webform\Plugin\WebformElement\OptionsBase::form

2 calls to TextBaseTrait::buildCounterForm()
OptionsBase::form in src/Plugin/WebformElement/OptionsBase.php
Gets the actual configuration webform array to be built.
TextBase::form in src/Plugin/WebformElement/TextBase.php
Gets the actual configuration webform array to be built.

File

src/Plugin/WebformElement/TextBaseTrait.php, line 24

Class

TextBaseTrait
Text base trait contains methods that are applicable to any text elements.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function buildCounterForm($name = '', $title = NULL) {
  if ($title === NULL) {
    $title = $this
      ->t('Counter');
  }
  $t_args = [
    '@title' => $title,
  ];
  $build[$name . 'counter_type'] = [
    '#type' => 'select',
    '#title' => $title,
    '#description' => $this
      ->t('Limit entered value to a maximum number of characters or words.'),
    '#empty_option' => $this
      ->t('- None -'),
    '#options' => [
      'character' => $this
        ->t('Characters'),
      'word' => $this
        ->t('Words'),
    ],
  ];
  $build['counter_container'] = $this
    ->getFormInlineContainer();
  $build['counter_container']['#states'] = [
    'invisible' => [
      ':input[name="properties[' . $name . 'counter_type]"]' => [
        'value' => '',
      ],
    ],
  ];
  $build['counter_container'][$name . 'counter_minimum'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('@title minimum', $t_args),
    '#min' => 1,
    '#states' => [
      'required' => [
        ':input[name="properties[' . $name . 'counter_type]"]' => [
          '!value' => '',
        ],
        ':input[name="properties[' . $name . 'counter_maximum]"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $build['counter_container'][$name . 'counter_maximum'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('@title maximum', $t_args),
    '#min' => 1,
    '#states' => [
      'required' => [
        ':input[name="properties[' . $name . 'counter_type]"]' => [
          '!value' => '',
        ],
        ':input[name="properties[' . $name . 'counter_minimum]"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $build['counter_message_container'] = [
    '#type' => 'container',
    '#states' => [
      'invisible' => [
        ':input[name="properties[' . $name . 'counter_type]"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $build['counter_message_container'][$name . 'counter_minimum_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('@title minimum message', $t_args),
    '#description' => $this
      ->t('Defaults to: %value', [
      '%value' => $this
        ->t('%d characters/word(s) entered'),
    ]),
    '#states' => [
      'visible' => [
        ':input[name="properties[' . $name . 'counter_minimum]"]' => [
          '!value' => '',
        ],
        ':input[name="properties[' . $name . 'counter_maximum]"]' => [
          'value' => '',
        ],
      ],
    ],
  ];
  $build['counter_message_container'][$name . 'counter_maximum_message'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('@title maximum message', $t_args),
    '#description' => $this
      ->t('Defaults to: %value', [
      '%value' => $this
        ->t('%d characters/word(s) remaining'),
    ]),
    '#states' => [
      'visible' => [
        ':input[name="properties[' . $name . 'counter_maximum]"]' => [
          '!value' => '',
        ],
      ],
    ],
  ];
  if ($this->librariesManager
    ->isExcluded('jquery.textcounter')) {
    $build[$name . 'counter_type']['#access'] = FALSE;
    $build[$name . 'counter_container']['#access'] = FALSE;
    $build[$name . 'counter_message_container']['#access'] = FALSE;
  }
  return $build;
}