You are here

public function ContextFormTrait::buildContextForm in Rules 8.3

Provides the form part for a context parameter.

2 calls to ContextFormTrait::buildContextForm()
ActionForm::form in src/Form/Expression/ActionForm.php
Adds elements specific to the expression to the form.
ConditionForm::form in src/Form/Expression/ConditionForm.php
Adds elements specific to the expression to the form.

File

src/Context/Form/ContextFormTrait.php, line 19

Class

ContextFormTrait
Provides form logic for handling contexts when configuring an expression.

Namespace

Drupal\rules\Context\Form

Code

public function buildContextForm(array $form, FormStateInterface $form_state, $context_name, ContextDefinitionInterface $context_definition, array $configuration) {
  $form['context_definitions'][$context_name] = [
    '#type' => 'fieldset',
    '#title' => $context_definition
      ->getLabel(),
  ];
  $form['context_definitions'][$context_name]['description'] = [
    '#markup' => $context_definition
      ->getDescription(),
  ];

  // If the form has been submitted already take the mode from the submitted
  // value, otherwise check for restriction setting, then check existing
  // configuration, and if that does not exist default to the "input" mode.
  $mode = $form_state
    ->get('context_' . $context_name);
  if (!$mode) {
    if ($mode = $context_definition
      ->getAssignmentRestriction()) {

      // If there is an assignmentRestriction value, use this for mode.
    }
    elseif (isset($configuration['context_mapping'][$context_name])) {
      $mode = ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_SELECTOR;
    }
    else {
      $mode = ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_INPUT;
    }
    $form_state
      ->set('context_' . $context_name, $mode);
  }
  $title = $mode == ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_SELECTOR ? $this
    ->t('Data selector') : $this
    ->t('Value');
  if (isset($configuration['context_values'][$context_name])) {
    $default_value = $configuration['context_values'][$context_name];
  }
  elseif (isset($configuration['context_mapping'][$context_name])) {
    $default_value = $configuration['context_mapping'][$context_name];
  }
  else {
    $default_value = $context_definition
      ->getDefaultValue();
  }
  $form['context_definitions'][$context_name]['setting'] = [
    '#type' => 'textfield',
    '#title' => $title,
    '#required' => $context_definition
      ->isRequired(),
    '#default_value' => $default_value,
  ];
  $element =& $form['context_definitions'][$context_name]['setting'];
  if ($mode == ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_SELECTOR) {
    $element['#description'] = $this
      ->t("The data selector helps you drill down into the available data. <em>To make entity fields appear in the data selector, you may have to use the condition 'Entity is of bundle'.</em> More useful tips about data selection are available in <a href=':url'>the online documentation</a>.", [
      ':url' => 'https://www.drupal.org/node/1300042',
    ]);
    $url = $this
      ->getRulesUiHandler()
      ->getUrlFromRoute('autocomplete', []);
    $element['#attributes']['class'][] = 'rules-autocomplete';
    $element['#attributes']['data-autocomplete-path'] = $url
      ->toString();
    $element['#attached']['library'][] = 'rules/rules.autocomplete';
  }
  elseif ($context_definition
    ->isMultiple()) {
    $element['#type'] = 'textarea';

    // @todo Get a description for possible values that can be filled in.
    $element['#description'] = $this
      ->t('Enter one value per line for this multi-valued context.');

    // Glue the list of values together as one item per line in the text area.
    if (is_array($default_value)) {
      $element['#default_value'] = implode("\n", $default_value);
    }
  }

  // If the context is not restricted to one mode or the other then provide a
  // button to switch between the two modes.
  if (empty($context_definition
    ->getAssignmentRestriction())) {
    $value = $mode == ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_SELECTOR ? $this
      ->t('Switch to the direct input mode') : $this
      ->t('Switch to data selection');
    $form['context_definitions'][$context_name]['switch_button'] = [
      '#type' => 'submit',
      '#name' => 'context_' . $context_name,
      '#attributes' => [
        'class' => [
          'rules-switch-button',
        ],
      ],
      '#parameter' => $context_name,
      '#value' => $value,
      '#submit' => [
        static::class . '::switchContextMode',
      ],
      // Do not validate!
      '#limit_validation_errors' => [],
    ];
  }
  return $form;
}