class ViewsRulesLoop in Views Rules 7
Loop plugin for using results from a view with Rules displays.
Hierarchy
- class \RulesExtendable extends \FacesExtendable
- class \RulesPlugin
- class \RulesContainerPlugin implements \IteratorAggregate
- class \RulesActionContainer implements RulesActionInterface
- class \ViewsRulesLoop implements ViewsRulesIterable
- class \RulesActionContainer implements RulesActionInterface
- class \RulesContainerPlugin implements \IteratorAggregate
- class \RulesPlugin
Expanded class hierarchy of ViewsRulesLoop
1 string reference to 'ViewsRulesLoop'
- views_rules_rules_plugin_info in ./
views_rules.rules.inc - Implements hook_rules_plugin_info().
File
- rules/
views_rules.plugin.inc, line 10 - Views Rules plugin implementations.
View source
class ViewsRulesLoop extends RulesActionContainer implements ViewsRulesIterable {
protected $itemName = 'view loop';
protected $view;
/**
* @var RulesState
*/
protected $viewLoopState;
public function __construct($viewName = NULL, $displayName = NULL, $settings = array()) {
$this->info += array(
'view_name' => $viewName,
'display_name' => $displayName,
);
$this->settings = (array) $settings + $this->settings;
$this
->setUp();
}
public function integrityCheck() {
// Check view is configured.
$view = $this
->getView();
if (!$view) {
throw new RulesIntegrityException(t('%plugin: View display is not configured.', array(
'%plugin' => $this
->getPluginName(),
)), $this);
}
// Check view display is an iterator.
$display = $view->display_handler;
if (!isset($display)) {
throw new RulesIntegrityException(t('%plugin: Configured view display is missing.', array(
'%plugin' => $this
->getPluginName(),
)), $this);
}
if (!$display instanceof views_rules_iterator) {
throw new RulesIntegrityException(t('%plugin: Configured view display is not a Rules iterator.', array(
'%plugin' => $this
->getPluginName(),
)), $this);
}
// Validate view display.
if (!$view
->validate()) {
throw new RulesIntegrityException(t('%plugin: Configured view does not validate.', array(
'%plugin' => $this
->getPluginName(),
)), $this);
}
parent::integrityCheck();
// Check row variables.
/** @var $display views_rules_iterator */
foreach ($display
->get_rules_variable_info() as $name => $info) {
if (isset($this->settings[$name . ':var'])) {
$this
->checkVarName($this->settings[$name . ':var']);
}
}
}
public function evaluate(RulesState $state) {
try {
if ($iterator = $this
->getViewIterator()) {
// Prepare view arguments.
$arguments = array_intersect_key($this
->getExecutionArguments($state), $this
->pluginParameterInfo());
$arguments = array_values(rules_unwrap_data($arguments));
// Execute view.
$this->viewLoopState = $state;
try {
$iterator
->execute_iterator($arguments, $this);
} catch (views_rules_iterator_exception $e) {
throw new RulesEvaluationException($e
->getMessage(), array(), $this);
}
unset($this->viewLoopState);
}
else {
throw new RulesEvaluationException('Unable to evaluate invalid view iterator.', array(), $this);
}
} catch (RulesEvaluationException $e) {
rules_log($e->msg, $e->args, $e->severity);
rules_log('Unable to evaluate %name.', array(
'%name' => $this
->getPluginName(),
), RulesLog::WARN, $this);
}
$this
->resetView();
}
public function evaluateRow(array $data) {
// Clone state to evaluate children in a sandbox.
$rowState = clone $this->viewLoopState;
// Fill in state data.
foreach ($this
->rowVariables() as $name => $info) {
$rowState
->addVariable($name, $data[$info['source name']], $info);
}
parent::evaluate($rowState);
// Update variables from parent scope.
foreach ($this->viewLoopState->variables as $key => &$value) {
if (array_key_exists($key, $rowState->variables)) {
$value = $rowState->variables[$key];
}
}
}
public function pluginParameterInfo() {
if ($iterator = $this
->getViewIterator()) {
return $iterator
->get_rules_parameter_info();
}
return array();
}
protected function stateVariables($element = NULL) {
$variables = parent::stateVariables($element);
// Add row variables to state.
$variables += $this
->rowVariables();
return $variables;
}
public function rowVariables() {
if ($iterator = $this
->getViewIterator()) {
$variables = array();
foreach ($iterator
->get_rules_variable_info() as $name => $info) {
if (isset($this->settings[$name . ':var'])) {
$variables[$this->settings[$name . ':var']] = array(
'source name' => $name,
'type' => $info['type'],
'label' => $this->settings[$name . ':label'],
);
}
}
return $variables;
}
// Return no variable.
return array();
}
/**
* @param bool $reset
* Whether to reset the iterator by retrieving a new one.
* @return view
*/
public function getView($reset = FALSE) {
if (!isset($this->view) || $reset) {
$view = views_get_view($this->info['view_name'], $reset);
if ($view && $view
->set_display($this->info['display_name'])) {
$this->view = $view;
}
}
return $this->view;
}
/**
* @param bool $reset
* Whether to reset the iterator by retrieving a new one.
* @return views_rules_iterator
*/
public function getViewIterator($reset = FALSE) {
if ($view = $this
->getView($reset)) {
return $view->display_handler instanceof views_rules_iterator ? $view->display_handler : NULL;
}
return NULL;
}
public function label() {
$view = $this
->getView();
return t('Views loop: @view - @display', array(
'@view' => ($viewLabel = $view
->get_human_name()) ? $viewLabel : $view->name,
'@display' => $view->display_handler->display->display_title,
));
}
public function dependencies() {
// TODO Remove once http://drupal.org/node/1682524 is fixed.
$modules = array_flip(RulesPlugin::dependencies());
$modules += array_flip(parent::dependencies());
return array_keys($modules);
}
protected function exportChildren($key = NULL) {
return parent::exportChildren('DO');
}
protected function exportFlat() {
return FALSE;
}
protected function exportSettings() {
$export = array();
$export['VIEW'] = $this->info['view_name'];
$export['DISPLAY'] = $this->info['display_name'];
$export += parent::exportSettings();
$export['ROW VARIABLES'] = array();
foreach ($this
->rowVariables() as $name => $info) {
$export['ROW VARIABLES'][$info['source name']][$name] = $info['label'];
}
return $export;
}
protected function importChildren($export, $key = NULL) {
parent::importChildren($export, 'DO');
}
protected function importSettings($export) {
// Import view.
$this->info['view_name'] = $export['VIEW'];
$this->info['display_name'] = $export['DISPLAY'];
// Import row variables.
foreach ($export['ROW VARIABLES'] as $name => $variable) {
$this->settings[$name . ':var'] = key($variable);
$this->settings[$name . ':label'] = current($variable);
}
parent::importSettings($export);
}
public function resetInternalCache() {
parent::resetInternalCache();
$this
->resetView();
}
public function resetView() {
$this->view = NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RulesActionContainer:: |
public | function | Adds an action to the container. | |
RulesActionContainer:: |
public | function | Returns an array of provided variable names. | |
RulesActionContainer:: |
protected | function |
Overrides RulesContainerPlugin:: |
1 |
RulesActionContainer:: |
public | function |
Applies the given export. Overrides RulesContainerPlugin:: |
1 |
RulesActionContainer:: |
public | function |
Returns info about variables 'provided' by the plugin. Overrides RulesPlugin:: |
|
RulesActionContainer:: |
public | function |
Returns info about all variables provided for later evaluated elements. Overrides RulesPlugin:: |
1 |
RulesContainerPlugin:: |
protected | property | ||
RulesContainerPlugin:: |
public | function |
Whether the currently logged in user has access to all configured elements. Overrides RulesPlugin:: |
1 |
RulesContainerPlugin:: |
public | function |
Returns info about variables available to be used as arguments for this element. Overrides RulesPlugin:: |
|
RulesContainerPlugin:: |
public | function | Returns the specified variables, in case the plugin is used as component. | |
RulesContainerPlugin:: |
public | function |
Overrides delete to keep the children alive, if possible. Overrides RulesPlugin:: |
1 |
RulesContainerPlugin:: |
public | function |
Removes circular object references so PHP garbage collector can work. Overrides RulesPlugin:: |
1 |
RulesContainerPlugin:: |
public | function |
Executes container with the given arguments. Overrides RulesPlugin:: |
1 |
RulesContainerPlugin:: |
public | function | Allows access to the children through the iterator. | 1 |
RulesContainerPlugin:: |
public | function |
Overrides optimize(). Overrides RulesPlugin:: |
|
RulesContainerPlugin:: |
public | function |
Returns info about parameters needed for executing the configured plugin. Overrides RulesPlugin:: |
|
RulesContainerPlugin:: |
protected | function |
Returns info about all variables that have to be setup in the state. Overrides RulesPlugin:: |
|
RulesContainerPlugin:: |
public | function | Sorts all child elements by their weight. | 1 |
RulesContainerPlugin:: |
protected | function |
Returns asserted additions to the available variable info. Overrides RulesPlugin:: |
1 |
RulesContainerPlugin:: |
public | function |
By default we do a deep clone. Overrides RulesPlugin:: |
1 |
RulesContainerPlugin:: |
public | function |
Overrides RulesPlugin:: |
2 |
RulesExtendable:: |
protected | property | ||
RulesExtendable:: |
public | function | ||
RulesExtendable:: |
public | function | Forces the object to be setUp, this executes setUp() if not done yet. | 1 |
RulesExtendable:: |
public static | function | Returns whether the a RuleExtendable supports the given interface. | |
RulesExtendable:: |
public | function | Allows items to add something to the rules cache. | 1 |
RulesExtendable:: |
protected | function | 1 | |
RulesExtendable:: |
public | function | Magic method: Invoke the dynamically implemented methods. | |
RulesPlugin:: |
protected | property | Static cache for availableVariables(). | 1 |
RulesPlugin:: |
protected | property |
Overrides RulesExtendable:: |
|
RulesPlugin:: |
protected | property | Identifies an element inside a configuration. | |
RulesPlugin:: |
protected | property |
Overrides RulesExtendable:: |
|
RulesPlugin:: |
public | property | If this is a configuration saved to the db, the id of it. | |
RulesPlugin:: |
protected | property | Info about this element. Usage depends on the plugin. | 2 |
RulesPlugin:: |
public | property | ||
RulesPlugin:: |
protected | property | The parent element, if any. | |
RulesPlugin:: |
public | property | An array of settings for this element. | |
RulesPlugin:: |
public | property | ||
RulesPlugin:: |
public | function | Applies the given data selector. | |
RulesPlugin:: |
protected | function | Checks whether parameters are correctly configured. | |
RulesPlugin:: |
protected | function | ||
RulesPlugin:: |
protected static | function | ||
RulesPlugin:: |
public | function | Returns the depth of this element in the configuration. | |
RulesPlugin:: |
public | function | Returns the element id, which identifies the element inside the config. | |
RulesPlugin:: |
public | function | Gets the element map helper object, which helps mapping elements to ids. | |
RulesPlugin:: |
public | function | Iterate over all elements nested below the current element. | |
RulesPlugin:: |
protected | function | Ensure the configuration has a name. If not, generate one. | |
RulesPlugin:: |
public | function | ||
RulesPlugin:: |
public | function | ||
RulesPlugin:: |
public | function | Execute the configuration. | |
RulesPlugin:: |
public | function | Exports a rule configuration. | |
RulesPlugin:: |
protected | function | ||
RulesPlugin:: |
public | function | Seamlessly invokes the method implemented via faces. | |
RulesPlugin:: |
public | function | ||
RulesPlugin:: |
public | function | ||
RulesPlugin:: |
protected | function | Returns the argument for the parameter $name described with $info. | |
RulesPlugin:: |
public | function | Returns info about the configured argument. | |
RulesPlugin:: |
protected | function | Gets the right arguments for executing the element. | |
RulesPlugin:: |
public | function | Gets the name of this plugin instance. | 1 |
RulesPlugin:: |
public | function | Checks if the configuration has a certain exportable status. | |
RulesPlugin:: |
public | function | Returns the config name. | |
RulesPlugin:: |
protected | function | ||
RulesPlugin:: |
public | function | Returns the info of the plugin. | 2 |
RulesPlugin:: |
public | function | ||
RulesPlugin:: |
public | function | Returns whether the element is the root of the configuration. | |
RulesPlugin:: |
public | function | Returns the element's parent. | |
RulesPlugin:: |
public | function | Returns the name of the element's plugin. | |
RulesPlugin:: |
public | function | Returns info about the element's plugin. | |
RulesPlugin:: |
public | function | Processes the settings e.g. to prepare input evaluators. | 1 |
RulesPlugin:: |
protected | function | Finalizes the configuration export. | |
RulesPlugin:: |
protected | function | Gets variables to return once the configuration has been executed. | 2 |
RulesPlugin:: |
public | function | Gets the root element of the configuration. | |
RulesPlugin:: |
public | function | Saves the configuration to the database. | 1 |
RulesPlugin:: |
public | function | Sets a new parent element. | |
RulesPlugin:: |
public | function | Sets up the execution state for the given arguments. | |
RulesPlugin:: |
public | function | When converted to a string, just use the export format. | |
ViewsRulesLoop:: |
protected | property |
The name of the item this class represents in the info hook. Overrides RulesExtendable:: |
|
ViewsRulesLoop:: |
protected | property | ||
ViewsRulesLoop:: |
protected | property | ||
ViewsRulesLoop:: |
public | function |
Calculates an array of required modules. Overrides RulesContainerPlugin:: |
|
ViewsRulesLoop:: |
public | function |
Evaluate, whereas by default new vars are visible in the parent's scope. Overrides RulesActionContainer:: |
|
ViewsRulesLoop:: |
public | function |
Evaluates a view row in the loop. Overrides ViewsRulesIterable:: |
|
ViewsRulesLoop:: |
protected | function |
Overrides RulesContainerPlugin:: |
|
ViewsRulesLoop:: |
protected | function |
Determines whether the element should be exported in flat style. Overrides RulesContainerPlugin:: |
|
ViewsRulesLoop:: |
protected | function |
Overrides RulesPlugin:: |
|
ViewsRulesLoop:: |
public | function | ||
ViewsRulesLoop:: |
public | function | ||
ViewsRulesLoop:: |
protected | function |
Overrides RulesContainerPlugin:: |
|
ViewsRulesLoop:: |
protected | function |
Overrides RulesPlugin:: |
|
ViewsRulesLoop:: |
public | function |
Makes sure the plugin is configured right. Overrides RulesContainerPlugin:: |
|
ViewsRulesLoop:: |
public | function |
Returns the label of the element. Overrides RulesPlugin:: |
|
ViewsRulesLoop:: |
public | function |
Returns info about parameters needed by the plugin. Overrides RulesPlugin:: |
|
ViewsRulesLoop:: |
public | function |
Resets any internal static caches. Overrides RulesContainerPlugin:: |
|
ViewsRulesLoop:: |
public | function | ||
ViewsRulesLoop:: |
public | function | ||
ViewsRulesLoop:: |
protected | function |
Returns available state variables for an element. Overrides RulesContainerPlugin:: |
|
ViewsRulesLoop:: |
public | function |
Overrides RulesActionContainer:: |