You are here

protected function ContextFormTrait::getContextConfigFromFormValues in Rules 8.3

Creates a context config object from the submitted form values.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: The form state containing the submitted values.

\Drupal\Core\Plugin\Context\ContextDefinitionInterface[] $context_definitions: The context definitions of the plugin.

Return value

\Drupal\rules\Context\ContextConfig The context config object populated with context mappings/values.

2 calls to ContextFormTrait::getContextConfigFromFormValues()
ActionForm::submitForm in src/Form/Expression/ActionForm.php
Form submission callback to save changes for the expression.
ConditionForm::submitForm in src/Form/Expression/ConditionForm.php
Form submission callback to save changes for the expression.

File

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

Class

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

Namespace

Drupal\rules\Context\Form

Code

protected function getContextConfigFromFormValues(FormStateInterface $form_state, array $context_definitions) {
  $context_config = ContextConfig::create();
  if ($form_state
    ->hasValue('context_definitions')) {
    foreach ($form_state
      ->getValue('context_definitions') as $context_name => $value) {
      if ($form_state
        ->get("context_{$context_name}") == ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_SELECTOR) {
        $context_config
          ->map($context_name, $value['setting']);
      }
      else {

        // Each line of the textarea is one value for 'multiple' contexts.
        if ($context_definitions[$context_name]
          ->isMultiple()) {

          // Textareas should always have \r\n line breaks, but for more
          // robust parsing we should also accommodate just \n or just \r.
          //
          // Additionally, we want to remove leading and trailing whitespace
          // from each line, and discard any empty lines.
          $values = preg_split('/\\s*\\R\\s*/', $value['setting'], NULL, PREG_SPLIT_NO_EMPTY);
          $context_config
            ->setValue($context_name, $values);
        }
        else {
          $context_config
            ->setValue($context_name, $value['setting']);
        }

        // For now, always add in the token context processor if it's present.
        // @todo Improve this in https://www.drupal.org/node/2804035.
        if ($this
          ->getDataProcessorManager()
          ->getDefinition('rules_tokens')) {
          $context_config
            ->process($context_name, 'rules_tokens');
        }
      }
    }
  }
  return $context_config;
}