You are here

public function BackgroundImageForm::buildSetting in Background Image 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/BackgroundImageForm.php \Drupal\background_image\Form\BackgroundImageForm::buildSetting()
  2. 2.x src/Form/BackgroundImageForm.php \Drupal\background_image\Form\BackgroundImageForm::buildSetting()

Builds a setting element.

Parameters

string $name: The base name of the setting.

array $element: A render array element used to construct the setting element.

Return value

array The group container for the newly created setting, passed by reference.

1 call to BackgroundImageForm::buildSetting()
BackgroundImageForm::buildSettings in src/Form/BackgroundImageForm.php
Builds the "Settings" group.

File

src/Form/BackgroundImageForm.php, line 422

Class

BackgroundImageForm

Namespace

Drupal\background_image\Form

Code

public function &buildSetting($name, array $element = []) {

  // Create the setting group.
  self::createGroup($name, $this->subform['settings'], [
    '#open' => !$this->inlineEntity,
    '#weight' => 11,
  ]);
  if (!$this->parent) {
    self::addState($this->subform['settings'][$name], 'visible', $this->subform['image'], [
      '*' => [
        'empty' => FALSE,
      ],
    ]);
  }

  // Move title and description into the fieldset.
  if (isset($element['#title'])) {
    $this->subform['settings'][$name]['#title'] = $element['#title'];
    if (isset($element['#type']) && ($element['#type'] === 'checkbox' || $element['#type'] === 'radio')) {
      $element['#title'] = $this
        ->t('Enabled');
    }
    else {
      unset($element['#title']);
    }
  }
  if (isset($element['#description'])) {
    $this->subform['settings'][$name]['#description'] = $element['#description'];
    unset($element['#description']);
  }

  // Provide an override toggle.
  $this->subform['settings'][$name]['toggle'] = [
    // Radios need to be rendered with a fieldset, setting a blank title will
    // force it and create the necessary data-drupal-selector attribute.
    '#title' => '',
    '#access' => !!$this->parent,
    '#type' => 'radios',
    '#options' => $this
      ->inheritOverrideOptions(),
    '#default_value' => $this
      ->getSubformValue([
      'settings',
      $name,
      'toggle',
    ], !$this->backgroundImage
      ->getSettings()
      ->isOverridden($name) && $this->parent ? BackgroundImageInterface::INHERIT : BackgroundImageInterface::NORMAL),
  ];

  // Provide a container to put settings in.
  $this->subform['settings'][$name]['container'] = [
    '#type' => 'container',
    '#parents' => array_merge($this->subform['settings'][$name]['#parents'], [
      'container',
    ]),
  ];
  if ($this->parent) {
    self::addState($this->subform['settings'][$name]['container'], 'visible', $this->subform['settings'][$name]['toggle'], [
      '[data-drupal-selector="%selector"] :input' => [
        'value' => BackgroundImageInterface::NORMAL,
      ],
    ]);
  }

  // Create the actual setting element.
  if (!isset($element['#default_value'])) {
    $element['#default_value'] = $this
      ->getSettingValue($name);
  }
  $this->subform['settings'][$name]['container']['value'] = $element;
  return $this->subform['settings'][$name];
}