You are here

Action.php in Business Rules 2.x

Same filename and directory in other branches
  1. 8 src/Entity/Action.php

File

src/Entity/Action.php
View source
<?php

namespace Drupal\business_rules\Entity;

use Drupal\business_rules\ActionInterface;
use Drupal\business_rules\BusinessRulesItemObject;
use Drupal\business_rules\Events\BusinessRulesEvent;

/**
 * Defines the Action entity.
 *
 * @ConfigEntityType(
 *   id = "business_rules_action",
 *   label = @Translation("Business Rules Action"),
 *   handlers = {
 *     "list_builder" = "Drupal\business_rules\ActionListBuilder",
 *     "form" = {
 *       "add" = "Drupal\business_rules\Form\ActionForm",
 *       "edit" = "Drupal\business_rules\Form\ActionForm",
 *       "delete" = "Drupal\business_rules\Form\ActionDeleteForm"
 *     },
 *     "route_provider" = {
 *       "html" = "Drupal\business_rules\ActionHtmlRouteProvider",
 *     },
 *   },
 *   config_prefix = "action",
 *   config_export = {
 *     "id",
 *     "type",
 *     "label",
 *     "description",
 *     "target_entity_type",
 *     "target_bundle",
 *     "tags",
 *     "settings"
 *   },
 *   admin_permission = "administer site configuration",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
 *     "uuid" = "uuid"
 *   },
 *   config_export = {
 *     "id",
 *     "label",
 *     "description",
 *     "settings",
 *     "tags",
 *     "uuid",
 *     "type",
 *     "target_entity_type",
 *     "target_bundle",
 *   },
 *   links = {
 *     "canonical" =
 *   "/admin/config/workflow/business_rules/action/{business_rules_action}",
 *     "add-form" = "/admin/config/workflow/business_rules/action/add",
 *     "edit-form" = "/admin/config/workflow/business_rules/action/{business_rules_action}/edit",
 *     "delete-form" = "/admin/config/workflow/business_rules/action/{business_rules_action}/delete",
 *     "collection" = "/admin/config/workflow/business_rules/action/collection/{view_mode}"
 *   }
 * )
 */
class Action extends BusinessRulesItemBase implements ActionInterface {

  /**
   * {@inheritdoc}
   */
  public function __construct(array $values, $entity_type = 'business_rules_action') {
    parent::__construct($values, $entity_type);
  }

  /**
   * {@inheritdoc}
   */
  public function getItemManager() {
    return \Drupal::getContainer()
      ->get('plugin.manager.business_rules.action');
  }

  /**
   * {@inheritdoc}
   */
  public function getBusinessRuleItemType() {
    return 'action';
  }

  /**
   * {@inheritdoc}
   */
  public function getBusinessRuleItemTranslatedType() {
    return t('Action');
  }

  /**
   * {@inheritdoc}
   */
  public function save() {

    // Prevent action to have the same name as one existent condition.
    $condition = Condition::load($this
      ->id());
    if (!empty($condition)) {
      $this->id = 'a_' . $this
        ->id();
    }
    return parent::save();
  }

  /**
   * {@inheritdoc}
   */
  public function execute(BusinessRulesEvent $event) {
    $action_type = $this->itemManager
      ->getDefinition($this
      ->getType());
    $reflection = new \ReflectionClass($action_type['class']);

    /** @var \Drupal\business_rules\Plugin\BusinessRulesActionPlugin $defined_action */
    $defined_action = $reflection
      ->newInstance($action_type, $action_type['id'], $action_type);
    $action = Action::load($this
      ->id());
    $defined_action
      ->processTokens($action, $event);
    return $defined_action
      ->execute($action, $event);
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();
    $items = $this
      ->getSettings('items');
    if (is_array($items)) {
      foreach (BusinessRulesItemObject::itemsArrayToItemsObject($items) as $item) {
        $this
          ->addDependency('config', $item
          ->loadEntity()
          ->getConfigDependencyName());
      }
    }
    return $this;
  }

}

Classes

Namesort descending Description
Action Defines the Action entity.