You are here

public static function ChangeFieldInfo::addFieldSubmit in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Plugin/BusinessRulesAction/ChangeFieldInfo.php \Drupal\business_rules\Plugin\BusinessRulesAction\ChangeFieldInfo::addFieldSubmit()

Add new field on action settings.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form_state.

File

src/Plugin/BusinessRulesAction/ChangeFieldInfo.php, line 75

Class

ChangeFieldInfo
Class ChangeFieldInfo.

Namespace

Drupal\business_rules\Plugin\BusinessRulesAction

Code

public static function addFieldSubmit(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\business_rules\Entity\Action $action */
  $action = $form_state
    ->get('action');
  $field = $form_state
    ->getValue('fields')['new.field'];
  $id = $field['field'] . '__' . $field['action'];
  $settings = $action
    ->getSettings();
  $availableFields = \Drupal::getContainer()
    ->get('business_rules.util')
    ->getBundleEditableFields($action
    ->getTargetEntityType(), $action
    ->getTargetBundle());
  $settings['fields'][$id] = [
    'id' => $id,
    'field' => $field['field'],
    'action' => $field['action'],
  ];
  uasort($settings['fields'], function ($a, $b) use ($availableFields) {
    return $availableFields[$a['field']] > $availableFields[$b['field']] ? 1 : -1;
  });
  $action
    ->setSetting('fields', $settings['fields']);
  $action
    ->save();
  $form_state
    ->setRedirect('entity.business_rules_action.edit_form', [
    'business_rules_action' => $action
      ->id(),
  ], [
    'fragment' => 'field-' . $id,
  ]);
}