You are here

public static function FormTrait::resetToDefault in Markdown 8.2

Allows a form element to be reset to its default value.

Parameters

array $element: The render array element to modify, passed by reference.

string $name: The name.

mixed $defaultValue: The default value.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

2 calls to FormTrait::resetToDefault()
ParserConfigurationForm::buildRenderStrategy in src/Form/ParserConfigurationForm.php
Builds the render strategy for a specific parser.
SettingsTrait::createSettingElement in src/Traits/SettingsTrait.php
Creates a setting element.

File

src/Traits/FormTrait.php, line 176

Class

FormTrait
Trait providing helpful methods when dealing with forms.

Namespace

Drupal\markdown\Traits

Code

public static function resetToDefault(array &$element, $name, $defaultValue, FormStateInterface $form_state) {

  /** @var \Drupal\markdown\Form\SubformStateInterface $form_state */
  $selector = static::getElementSelector($name, $form_state
    ->createParents());
  $reset = FormTrait::createElement([
    '#type' => 'link',
    '#title' => '↩️',
    '#url' => Url::fromUserInput('#reset-default-value', [
      'external' => TRUE,
    ]),
    '#attributes' => [
      'data' => [
        'markdownElement' => 'reset',
        'markdownId' => 'reset_' . $name,
        'markdownTarget' => $selector,
        'markdownDefaultValue' => $defaultValue,
      ],
      'title' => t('Reset to default value'),
      'style' => 'display: none; text-decoration: none;',
    ],
  ]);

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $element['#attached']['library'][] = 'markdown/reset';
  $element['#title'] = new FormattableMarkup('@title @reset', [
    '@title' => $element['#title'],
    '@reset' => $renderer
      ->renderPlain($reset),
  ]);
}