public function PublishContent::execute in Business Rules 8
Same name and namespace in other branches
- 2.x src/Plugin/BusinessRulesAction/PublishContent.php \Drupal\business_rules\Plugin\BusinessRulesAction\PublishContent::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/ PublishContent.php, line 56
Class
- PublishContent
- Class PublishContent.
Namespace
Drupal\business_rules\Plugin\BusinessRulesActionCode
public function execute(ActionInterface $action, BusinessRulesEvent $event) {
// Get settings.
$entity_type = $action
->getTargetEntityType();
$bundle = $action
->getTargetBundle();
$field = $action
->getSettings('field');
$value = $action
->getSettings('value');
$value = $this
->processVariables($value, $event
->getArgument('variables'));
// Load entities ids to Publish.
/** @var \Drupal\Core\Entity\Query\QueryInterface $query_service */
/** @var \Drupal\Core\Entity\Query\Sql\Query $query */
$query_service = \Drupal::getContainer()
->get('entity.query');
$query = $query_service
->get($entity_type);
$query
->condition('type', $bundle);
$query
->condition($field, $value);
$ids = $query
->execute();
// Publish entities.
/** @var \Drupal\Core\Entity\EntityStorageInterface $entityManager */
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entityManager = \Drupal::entityTypeManager()
->getStorage($entity_type);
$entities = $entityManager
->loadMultiple($ids);
$key_value = \Drupal::keyValueExpirable('business_rules.publish_entity');
foreach ($entities as $entity) {
$unpublished_id = $key_value
->get($entity
->id());
// Prevent infinite calls regarding the dispatched entity events such as
// save / presave, etc.
if ($unpublished_id != $entity
->id()) {
$key_value
->set($entity
->id(), $entity
->id());
$entity->status
->setValue(1);
$entity
->save();
}
}
$result = [
'#type' => 'markup',
'#markup' => t('Publish entities with ids %ids.', [
'%ids' => implode(',', $ids),
]),
];
return $result;
}