public function BusinessRuleForm::save in Business Rules 8
Same name and namespace in other branches
- 2.x src/Form/BusinessRuleForm.php \Drupal\business_rules\Form\BusinessRuleForm::save()
Form submission handler for the 'save' action.
Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.
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
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityForm::save
File
- src/
Form/ BusinessRuleForm.php, line 481
Class
- BusinessRuleForm
- Class BusinessRuleForm.
Namespace
Drupal\business_rules\FormCode
public function save(array $form, FormStateInterface $form_state) {
if ($this->step < 2 && $this->entity
->isNew()) {
$this->step++;
$form_state
->setRebuild();
return $form;
}
else {
/** @var \Drupal\business_rules\Entity\BusinessRule $business_rule */
$business_rule = $this->entity;
$items = $form_state
->getValue('items');
$business_rule
->set('items', []);
$br_items = [];
if (is_array($items)) {
foreach ($items as $key => $value) {
$itemObj = new BusinessRulesItemObject($key, $value['business_rule_item_type'], $value['weight']);
$br_items[$value['weight']] = $itemObj;
}
}
ksort($br_items);
foreach ($br_items as $item) {
$business_rule
->addItem($item);
}
$business_rule
->setTags(explode(',', $form_state
->getValue('tags')));
$status = $business_rule
->save();
// As the rule may need to be executed under a cached hook, we need to
// invalidate all rendered caches.
Cache::invalidateTags([
'rendered',
]);
switch ($status) {
case SAVED_NEW:
drupal_set_message($this
->t('Created the %label Rule.', [
'%label' => $business_rule
->label(),
]));
break;
default:
drupal_set_message($this
->t('Saved the %label Rule.', [
'%label' => $business_rule
->label(),
]));
}
if (isset($form_state
->getTriggeringElement()['#op'])) {
$op = $form_state
->getTriggeringElement()['#op'];
if ($op == 'save') {
$form_state
->setRedirectUrl($business_rule
->urlInfo('edit-form', [
'business_rule' => $business_rule
->id(),
]));
}
else {
$form_state
->setRedirectUrl($business_rule
->urlInfo('collection'));
}
}
}
return $status;
}