public function SetFieldValue::execute in Business Rules 2.x
Same name and namespace in other branches
- 8 src/Plugin/BusinessRulesAction/SetFieldValue.php \Drupal\business_rules\Plugin\BusinessRulesAction\SetFieldValue::execute()
Execute the action.
Parameters
\Drupal\business_rules\ActionInterface $action: The configured action.
\Drupal\business_rules\Events\BusinessRulesEvent $event: The event that has triggered the action.
Return value
array The render array to be showed on debug block.
Overrides BusinessRulesActionPlugin::execute
File
- src/
Plugin/ BusinessRulesAction/ SetFieldValue.php, line 54
Class
- SetFieldValue
- Class SetFieldValue.
Namespace
Drupal\business_rules\Plugin\BusinessRulesActionCode
public function execute(ActionInterface $action, BusinessRulesEvent $event) {
/** @var \Drupal\Core\Entity\Entity $entity */
$variables = $event
->getArgument('variables');
$field = $action
->getSettings('field');
$raw_value = $action
->getSettings('value');
$value = $this
->processVariables($raw_value, $variables);
$entity = $event
->getArgument('entity');
$cardinality = $entity->{$field}
->getFieldDefinition()
->getFieldStorageDefinition()
->getCardinality();
// Set value to multi-valor field.
if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || $cardinality > 1) {
$arr = explode(chr(10) . '|', $value);
if (substr($arr[0], 0, 1) == '|') {
$arr[0] = substr($arr[0], 1, strlen($arr[0]) - 1);
}
foreach ($arr as $key => $value) {
if (substr($value, strlen($value) - 1, 1) == "\r") {
$arr[$key] = substr($value, 0, strlen($value) - 1);
}
$arr[$key . '000000'] = $this
->processVariables($arr[$key], $variables);
unset($arr[$key]);
}
// Put all values at the array root.
foreach ($arr as $key => $item) {
if (is_array($item)) {
unset($arr[$key]);
foreach ($item as $new_key => $new_item) {
$arr[$key + $new_key] = $new_item;
}
}
}
ksort($arr);
$value = $arr;
}
$entity->{$field}
->setValue($value);
$result = [
'#type' => 'markup',
'#markup' => t('Entity %entity updated. Field: %field, value: %value', [
'%entity' => $entity
->getEntityTypeId(),
'%field' => $field,
'%value' => is_array($value) ? implode(',', $value) : $value,
]),
];
return $result;
}