function public function SettingBase::getSettingElement

8.x-3.x SettingBase.php public SettingBase::getSettingElement(Element $form, FormStateInterface $form_state)

Retrieves the form element for the setting.

Parameters

\Drupal\bootstrap\Utility\Element $form: The Element object that comprises the form.

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

Return value

\Drupal\bootstrap\Utility\Element The setting element object.

Overrides SettingInterface::getSettingElement

Class

SettingBase
Base class for a setting.

Namespace

Drupal\bootstrap\Plugin\Setting

Source src/Plugin/Setting/SettingBase.php (line 175)

public function getSettingElement(Element $form, FormStateInterface $form_state) {
  // Construct the group elements.
  $group = $this->getGroupElement($form, $form_state);
  $plugin_id = $this->getPluginId();
  if (!isset($group->$plugin_id)) {
    // Set properties from the plugin definition.
    foreach ($this->getElementProperties() as $name => $value) {
      $group->$plugin_id->setProperty($name, $value);
    }

    // Get the default value.
    $default_value = $this->getSettingValue($form_state);

    // Convert value from an array into a newline separated value.
    // @todo Remove once settings have proper config schemas in place.
    // @see https://www.drupal.org/project/bootstrap/issues/2883714
    if ($group->$plugin_id->getProperty('type') === 'textarea' && is_array($default_value)) {
      $default_value = implode("\n", $default_value);
    }

    // Set default value from the stored form state value or theme setting.
    $group->$plugin_id->setProperty('default_value', $default_value);

    // Append additional "see" link references to the description.
    $description = (string) $group->$plugin_id->getProperty('description') ? : '';
    $links = [];
    foreach ($this->pluginDefinition['see'] as $url => $title) {
      $link = Element::createStandalone([
        '#type' => 'link',
        '#url' => Url::fromUri($url),
        '#title' => $title,
        '#attributes' => [
          'target' => '_blank',
        ],
      ], $form_state);
      $links[] = (string) $link->renderPlain();
    }
    if (!empty($links)) {
      $description .= '<br>';
      $description .= t('See also:');
      $description .= ' ' . implode(', ', $links);
      $group->$plugin_id->setProperty('description', $description);
    }
  }

  // Set accessibility.
  $group->$plugin_id->access($this->access());

  return $group->$plugin_id;
}