class RulesAction

Same name in other branches
  1. 8.x-3.x src/Core/Annotation/RulesAction.php \Drupal\rules\Core\Annotation\RulesAction

Implements a rules action.

Hierarchy

Expanded class hierarchy of RulesAction

1 string reference to 'RulesAction'
rules_rules_plugin_info in ./rules.module
Implements hook_rules_plugin_info().

File

includes/rules.plugins.inc, line 11

View source
class RulesAction extends RulesAbstractPlugin implements RulesActionInterface {
    
    /**
     * @var string
     */
    protected $itemName = 'action';
    
    /**
     * Execute the callback and update/save data as specified by the action.
     */
    protected function executeCallback(array $args, RulesState $state = NULL) {
        rules_log('Evaluating the action %name.', array(
            '%name' => $this->label($this->elementName),
        ), RulesLog::INFO, $this);
        $return = $this->__call('execute', empty($this->info['named parameter']) ? $args : array(
            $args,
        ));
        // Get the (partially) wrapped arguments.
        $args = $state->currentArguments;
        if (is_array($return)) {
            foreach ($return as $name => $data) {
                // Add provided variables.
                if (isset($this->info['provides'][$name])) {
                    $var_name = isset($this->settings[$name . ':var']) ? $this->settings[$name . ':var'] : $name;
                    if (!$state->varInfo($var_name)) {
                        $state->addVariable($var_name, $data, $this->info['provides'][$name]);
                        rules_log('Added the provided variable %name of type %type', array(
                            '%name' => $var_name,
                            '%type' => $this->info['provides'][$name]['type'],
                        ), RulesLog::INFO, $this);
                        if (!empty($this->info['provides'][$name]['save']) && $state->variables[$var_name] instanceof EntityMetadataWrapper) {
                            $state->saveChanges($var_name, $state->variables[$var_name]);
                        }
                    }
                }
                elseif (!isset($this->info['provides'][$name])) {
                    // Update the data value using the wrapper.
                    if (isset($args[$name]) && $args[$name] instanceof EntityMetadataWrapper) {
                        try {
                            $args[$name]->set($data);
                        } catch (EntityMetadataWrapperException $e) {
                            throw new RulesEvaluationException('Unable to update the argument for parameter %name: %error', array(
                                '%name' => $name,
                                '%error' => $e->getMessage(),
                            ), $this);
                        }
                    }
                    elseif (array_key_exists($name, $args)) {
                        // Map back to the source variable name and update it.
                        $var_name = !empty($this->settings[$name . ':select']) ? str_replace('-', '_', $this->settings[$name . ':select']) : $name;
                        $state->variables[$var_name] = $data;
                    }
                }
            }
        }
        // Save parameters as defined in the parameter info.
        if ($return !== FALSE) {
            foreach ($this->info['parameter'] as $name => $info) {
                if (!empty($info['save']) && $args[$name] instanceof EntityMetadataWrapper) {
                    if (isset($this->settings[$name . ':select'])) {
                        $state->saveChanges($this->settings[$name . ':select'], $args[$name]);
                    }
                    else {
                        // Wrapper has been configured via direct input, so just save.
                        rules_log('Saved argument of type %type for parameter %name.', array(
                            '%name' => $name,
                            '%type' => $args[$name]->type(),
                        ));
                        $args[$name]->save();
                    }
                }
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
RulesAbstractPlugin::$elementName protected property
RulesAbstractPlugin::$info protected property Info about this element. Usage depends on the plugin. Overrides RulesPlugin::$info
RulesAbstractPlugin::$infoLoaded protected property
RulesAbstractPlugin::access public function Whether the currently logged in user has access to all configured elements. Overrides RulesPlugin::access
RulesAbstractPlugin::dependencies public function Calculates an array of required modules. Overrides RulesPlugin::dependencies
RulesAbstractPlugin::evaluate public function Evaluate the element on a given rules evaluation state. Overrides RulesPlugin::evaluate
RulesAbstractPlugin::executeByArgs public function Execute the configuration by passing arguments in a single array. Overrides RulesPlugin::executeByArgs
RulesAbstractPlugin::exportToArray protected function Overrides RulesPlugin::exportToArray 1
RulesAbstractPlugin::forceSetUp public function Forces the object to be setUp, this executes setUp() if not done yet. Overrides RulesExtendable::forceSetUp
RulesAbstractPlugin::getElementName public function Gets the name of the configured action or condition.
RulesAbstractPlugin::getFileName protected function
RulesAbstractPlugin::getIncludeFiles protected static function Returns all include files for a module.
RulesAbstractPlugin::getPluginName public function Gets the name of this plugin instance. Overrides RulesPlugin::getPluginName
RulesAbstractPlugin::import public function Applies the given export. Overrides RulesPlugin::import 1
RulesAbstractPlugin::includeFiles public static function Makes sure all supported destinations are included.
RulesAbstractPlugin::info public function Returns the info of the plugin. Overrides RulesPlugin::info
RulesAbstractPlugin::integrityCheck public function Makes sure the plugin is configured right. Overrides RulesPlugin::integrityCheck
RulesAbstractPlugin::label public function Returns the label of the element. Overrides RulesPlugin::label 1
RulesAbstractPlugin::loadBasicInclude protected function Loads this module's .rules.inc file.
RulesAbstractPlugin::pluginParameterInfo public function Returns info about parameters needed by the plugin. Overrides RulesPlugin::pluginParameterInfo
RulesAbstractPlugin::pluginProvidesVariables public function Returns info about variables 'provided' by the plugin. Overrides RulesPlugin::pluginProvidesVariables
RulesAbstractPlugin::processSettings public function Processes the settings e.g. to prepare input evaluators. Overrides RulesPlugin::processSettings
RulesAbstractPlugin::rebuildCache public function Add in the data provided by the info hooks to the cache. Overrides RulesExtendable::rebuildCache
RulesAbstractPlugin::setUp protected function Overrides RulesExtendable::setUp
RulesAbstractPlugin::variableInfoAssertions protected function Returns asserted additions to the available variable info. Overrides RulesPlugin::variableInfoAssertions
RulesAbstractPlugin::__construct public function Overrides RulesExtendable::__construct
RulesAbstractPlugin::__sleep public function Overrides RulesPlugin::__sleep 1
RulesAction::$itemName protected property Overrides RulesExtendable::$itemName
RulesAction::executeCallback protected function Execute the callback and update/save data as specified by the action. Overrides RulesAbstractPlugin::executeCallback
RulesExtendable::$itemInfo protected property
RulesExtendable::facesAs public function
RulesExtendable::itemFacesAs public static function Returns whether the a RuleExtendable supports the given interface.
RulesExtendable::__call public function Magic method: Invoke the dynamically implemented methods.
RulesPlugin::$availableVariables protected property Static cache for availableVariables(). 1
RulesPlugin::$cache protected property Overrides RulesExtendable::$cache
RulesPlugin::$elementId protected property Identifies an element inside a configuration.
RulesPlugin::$hook protected property Overrides RulesExtendable::$hook
RulesPlugin::$id public property If this is a configuration saved to the db, the id of it.
RulesPlugin::$name public property
RulesPlugin::$parent protected property The parent element, if any.
RulesPlugin::$settings public property An array of settings for this element.
RulesPlugin::$weight public property
RulesPlugin::applyDataSelector public function Applies the given data selector.
RulesPlugin::availableVariables public function Returns info about variables available to be used as arguments for this element. 1
RulesPlugin::checkParameterSettings protected function Checks whether parameters are correctly configured.
RulesPlugin::checkVarName protected function
RulesPlugin::compare protected static function
RulesPlugin::delete public function Deletes configuration from database. 1
RulesPlugin::depth public function Returns the depth of this element in the configuration.
RulesPlugin::destroy public function Removes circular object references so PHP garbage collector can work. 1
RulesPlugin::elementId public function Returns the element id, which identifies the element inside the config.
RulesPlugin::elementMap public function Gets the element map helper object, which helps mapping elements to ids.
RulesPlugin::elements public function Iterate over all elements nested below the current element.
RulesPlugin::ensureNameExists protected function Ensure the configuration has a name. If not, generate one.
RulesPlugin::entityInfo public function
RulesPlugin::entityType public function
RulesPlugin::execute public function Execute the configuration.
RulesPlugin::export public function Exports a rule configuration.
RulesPlugin::exportParameterSetting protected function
RulesPlugin::exportSettings protected function 1
RulesPlugin::form public function Seamlessly invokes the method implemented via faces.
RulesPlugin::form_submit public function
RulesPlugin::form_validate public function
RulesPlugin::getArgument protected function Returns the argument for the parameter $name described with $info.
RulesPlugin::getArgumentInfo public function Returns info about the configured argument.
RulesPlugin::getExecutionArguments protected function Gets the right arguments for executing the element.
RulesPlugin::hasStatus public function Checks if the configuration has a certain exportable status.
RulesPlugin::identifier public function Returns the config name.
RulesPlugin::importParameterSetting protected function
RulesPlugin::importSettings protected function 1
RulesPlugin::internalIdentifier public function
RulesPlugin::isRoot public function Returns whether the element is the root of the configuration.
RulesPlugin::optimize public function Optimizes a rule configuration in order to speed up evaluation. 1
RulesPlugin::parameterInfo public function Returns info about parameters needed for executing the configured plugin. 1
RulesPlugin::parentElement public function Returns the element's parent.
RulesPlugin::plugin public function Returns the name of the element's plugin.
RulesPlugin::pluginInfo public function Returns info about the element's plugin.
RulesPlugin::providesVariables public function Returns info about all variables provided for later evaluated elements. 2
RulesPlugin::resetInternalCache public function Resets any internal static caches. 1
RulesPlugin::returnExport protected function Finalizes the configuration export.
RulesPlugin::returnVariables protected function Gets variables to return once the configuration has been executed. 2
RulesPlugin::root public function Gets the root element of the configuration.
RulesPlugin::save public function Saves the configuration to the database. 1
RulesPlugin::setParent public function Sets a new parent element.
RulesPlugin::setUpState public function Sets up the execution state for the given arguments.
RulesPlugin::setUpVariables protected function Returns info about all variables that have to be setup in the state. 1
RulesPlugin::__clone public function Do a deep clone. 1
RulesPlugin::__toString public function When converted to a string, just use the export format.