You are here

function length_indicator_field_widget_third_party_settings_form in Length Indicator 8

Implements hook_field_widget_third_party_settings_form().

File

./length_indicator.module, line 44
Length Indicator module hooks.

Code

function length_indicator_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
  $element = [];
  if (_length_indicator_widget_is_supported($plugin
    ->getPluginId())) {
    $element['indicator'] = [
      '#type' => 'checkbox',
      '#title' => t('Length indicator'),
      '#default_value' => $plugin
        ->getThirdPartySetting('length_indicator', 'indicator'),
    ];
    $element['indicator_opt'] = [
      '#type' => 'fieldset',
      '#title' => t('Length indicator settings'),
      '#states' => [
        'visible' => [
          ':input[name="fields[' . $field_definition
            ->getName() . '][settings_edit_form][third_party_settings][length_indicator][indicator]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $indicator_opt = $plugin
      ->getThirdPartySetting('length_indicator', 'indicator_opt');
    $element['indicator_opt']['optimin'] = [
      '#type' => 'number',
      '#title' => t('Optimum minimum'),
      '#default_value' => isset($indicator_opt['optimin']) ? $indicator_opt['optimin'] : 10,
      '#required' => TRUE,
      '#min' => 1,
    ];
    $element['indicator_opt']['optimax'] = [
      '#type' => 'number',
      '#title' => t('Optimum maximum'),
      '#default_value' => isset($indicator_opt['optimax']) ? $indicator_opt['optimax'] : 15,
      '#required' => TRUE,
      '#min' => 5,
      '#element_validate' => [
        '_length_indicator_settings_optimax',
      ],
    ];
    $element['indicator_opt']['tolerance'] = [
      '#type' => 'number',
      '#title' => t('Tolerance'),
      '#default_value' => isset($indicator_opt['tolerance']) ? $indicator_opt['tolerance'] : 5,
      '#required' => TRUE,
      '#min' => 0,
      '#element_validate' => [
        '_length_indicator_settings_tolerance',
      ],
    ];
  }
  return $element;
}