You are here

protected function SettingsTrait::createSettingElement in Markdown 8.2

Creates a setting element.

Parameters

string $name: The setting name.

array $element: The array element to construct. Note: this will be filled in with defaults if they're not provided.

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

callable $valueTransformer: Optional. Callback used to transform the setting value.

Return value

array A render array with a child matching the name of the setting. This is primarily so that it can union with the parent element, e.g. `$form += $this->createSettingsElement(...)`.

10 calls to SettingsTrait::createSettingElement()
CommonMark::buildConfigurationForm in src/Plugin/Markdown/CommonMark/CommonMark.php
Form constructor.
EmojiExtension::buildConfigurationForm in src/Plugin/Markdown/CommonMark/Extension/EmojiExtension.php
Form constructor.
ExternalLinkExtension::buildConfigurationForm in src/Plugin/Markdown/CommonMark/Extension/ExternalLinkExtension.php
Form constructor.
FootnoteExtension::buildConfigurationForm in src/Plugin/Markdown/CommonMark/Extension/FootnoteExtension.php
Form constructor.
HeadingPermalinkExtension::buildConfigurationForm in src/Plugin/Markdown/CommonMark/Extension/HeadingPermalinkExtension.php
Form constructor.

... See full list

File

src/Traits/SettingsTrait.php, line 35

Class

SettingsTrait
Trait for installable plugins that implement settings.

Namespace

Drupal\markdown\Traits

Code

protected function createSettingElement($name, array $element, FormStateInterface $form_state, callable $valueTransformer = NULL) {

  /** @var \Drupal\markdown\Form\SubformStateInterface $form_state */
  $settingName = $name;
  $parts = explode('.', $name);
  $name = array_pop($parts);

  // Prevent render if setting doesn't exist.
  if (!isset($element['#access']) && !$this
    ->settingExists($settingName)) {
    $element['#access'] = FALSE;
  }

  // Create placeholder title so it can be replaced with a proper translation.
  if (!isset($element['#title'])) {
    @trigger_error('Deprecated in markdown:8.x-2.0 and is removed from markdown:3.0.0. No replacement for automatically populating. The #title property must be explicitly specified for locale tools to extract the correct value. See https://www.drupal.org/project/markdown/issues/3142418.', E_USER_DEPRECATED);
    $element['#title'] = $this
      ->t(ucwords(str_replace([
      '-',
      '_',
    ], ' ', $name)));

    // phpcs:ignore
  }

  // Handle initial setting value (Drupal names this #default_value).
  if (!isset($element['#default_value'])) {
    $value = $form_state
      ->getValue($name, $this
      ->getSetting($settingName));
    if ($valueTransformer) {
      $return = call_user_func($valueTransformer, $value);
      if (isset($return)) {
        $value = $return;
      }
    }
    $element['#default_value'] = $value;
  }

  // Handle real default setting value.
  $defaultValue = $this
    ->getDefaultSetting($settingName);
  if (isset($defaultValue)) {
    if ($valueTransformer) {
      $return = call_user_func($valueTransformer, $defaultValue);
      if (isset($return)) {
        $defaultValue = $return;
      }
    }
    FormTrait::resetToDefault($element, $name, $defaultValue, $form_state);
  }
  return [
    $name => FormTrait::createElement($element),
  ];
}