You are here

protected function AssetInjectorFormBase::buildConditionsInterface in Asset Injector 8.2

Helper function for building the conditions UI form.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

array The form array with the conditions UI added in.

1 call to AssetInjectorFormBase::buildConditionsInterface()
AssetInjectorFormBase::form in src/Form/AssetInjectorFormBase.php
Gets the actual form array to be built.

File

src/Form/AssetInjectorFormBase.php, line 198

Class

AssetInjectorFormBase
Class AssetInjectorCsssForm.

Namespace

Drupal\asset_injector\Form

Code

protected function buildConditionsInterface(array $form, FormStateInterface $form_state) {
  $form['conditions_tabs'] = [
    '#type' => 'vertical_tabs',
    '#title' => $this
      ->t('Conditions'),
    '#parents' => [
      'conditions_tabs',
    ],
    '#attached' => [
      'library' => [
        'asset_injector/asset_injector',
      ],
    ],
  ];

  // @todo Allow list of conditions to be configured in
  //   https://www.drupal.org/node/2284687.
  $conditions = $this->entity
    ->getConditions();
  foreach ($this->manager
    ->getDefinitionsForContexts($form_state
    ->getTemporaryValue('gathered_contexts')) as $condition_id => $definition) {

    // Don't display the language condition until we have multiple languages.
    if ($condition_id == 'language' && !$this->language
      ->isMultilingual()) {
      continue;
    }
    $condition_config = isset($conditions[$condition_id]) ? $conditions[$condition_id] : [];

    /** @var \Drupal\Core\Condition\ConditionInterface $condition */
    $condition = $this->manager
      ->createInstance($condition_id, $condition_config);
    $form_state
      ->set([
      'conditions',
      $condition_id,
    ], $condition);
    $condition_form = $condition
      ->buildConfigurationForm([], $form_state);
    $condition_form['#type'] = 'details';
    $condition_form['#title'] = $condition
      ->getPluginDefinition()['label'];
    $condition_form['#group'] = 'conditions_tabs';
    if ($condition_id == 'current_theme') {
      $condition_form['theme']['#multiple'] = TRUE;
    }
    $form[$condition_id] = $condition_form;
  }

  // Modify the titles of the node_type plugin & hide negate.
  if (isset($form['node_type'])) {
    $form['node_type']['#title'] = $this
      ->t('Content types');
    $form['node_type']['bundles']['#title'] = $this
      ->t('Content types');
    $form['node_type']['negate']['#type'] = 'hidden';
    $form['node_type']['negate']['#value'] = $form['node_type']['negate']['#default_value'];
  }

  // Modify the request_path negate to a radio button.
  if (isset($form['request_path'])) {
    $form['request_path']['#title'] = $this
      ->t('Pages');
    $form['request_path']['negate']['#type'] = 'radios';
    $form['request_path']['negate']['#default_value'] = (int) $form['request_path']['negate']['#default_value'];
    $form['request_path']['negate']['#title_display'] = 'invisible';
    $form['request_path']['negate']['#options'] = [
      $this
        ->t('Show for the listed pages'),
      $this
        ->t('Hide for the listed pages'),
    ];
  }
  return $form;
}