class RulesEventHandlerEntityBundle
Exposes the bundle of an entity as event setting.
Hierarchy
- class \RulesEventHandlerBase implements \RulesEventHandlerInterface
- class \RulesEventHandlerEntityBundle extends \RulesEventHandlerBase
Expanded class hierarchy of RulesEventHandlerEntityBundle
1 string reference to 'RulesEventHandlerEntityBundle'
- rules.module in ./
rules.module - Rules engine module.
File
-
includes/
rules.event.inc, line 311
View source
class RulesEventHandlerEntityBundle extends RulesEventHandlerBase {
protected $entityType;
protected $entityInfo;
protected $bundleKey;
/**
* Implements RulesEventHandlerInterface::__construct().
*/
public function __construct($event_name, $info) {
parent::__construct($event_name, $info);
// Cut off the suffix, e.g. remove 'view' from node_view.
$this->entityType = implode('_', explode('_', $event_name, -1));
$this->entityInfo = entity_get_info($this->entityType);
if (!$this->entityInfo) {
throw new InvalidArgumentException('Unsupported event name passed.');
}
}
/**
* Implements RulesEventHandlerInterface::summary().
*/
public function summary() {
$bundle =& $this->settings['bundle'];
$bundle_label = isset($this->entityInfo['bundles'][$bundle]['label']) ? $this->entityInfo['bundles'][$bundle]['label'] : $bundle;
$suffix = isset($bundle) ? ' ' . t('of @bundle-key %name', array(
'@bundle-key' => $this->getBundlePropertyLabel(),
'%name' => $bundle_label,
)) : '';
return check_plain($this->eventInfo['label']) . $suffix;
}
/**
* Implements RulesEventHandlerInterface::buildForm().
*/
public function buildForm(array &$form_state) {
$form['bundle'] = array(
'#type' => 'select',
'#title' => t('Restrict by @bundle', array(
'@bundle' => $this->getBundlePropertyLabel(),
)),
'#description' => t('If you need to filter for multiple values, either add multiple events or use the "Entity is of bundle" condition instead.'),
'#default_value' => $this->settings['bundle'],
'#empty_value' => '',
'#options' => array(),
);
foreach ($this->entityInfo['bundles'] as $name => $bundle_info) {
$form['bundle']['#options'][$name] = $bundle_info['label'];
}
return $form;
}
/**
* Returns the label to use for the bundle property.
*
* @return string
* The label to use for the bundle property.
*/
protected function getBundlePropertyLabel() {
return $this->entityInfo['entity keys']['bundle'];
}
/**
* Implements RulesEventHandlerInterface::extractFormValues().
*/
public function extractFormValues(array &$form, array &$form_state) {
$this->settings['bundle'] = !empty($form_state['values']['bundle']) ? $form_state['values']['bundle'] : NULL;
}
/**
* Implements RulesEventHandlerInterface::validate().
*/
public function validate() {
if ($this->settings['bundle'] && empty($this->entityInfo['bundles'][$this->settings['bundle']])) {
throw new RulesIntegrityException(t('The @bundle %bundle of %entity_type is not known.', array(
'%bundle' => $this->settings['bundle'],
'%entity_type' => $this->entityInfo['label'],
'@bundle' => $this->getBundlePropertyLabel(),
)), array(
NULL,
'bundle',
));
}
}
/**
* Implements RulesEventHandlerInterface::getConfiguredEventName().
*/
public function getEventNameSuffix() {
return $this->settings['bundle'];
}
/**
* Implements RulesEventHandlerInterface::getDefaults().
*/
public function getDefaults() {
return array(
'bundle' => NULL,
);
}
/**
* Implements RulesEventHandlerInterface::availableVariables().
*/
public function availableVariables() {
$variables = $this->eventInfo['variables'];
if ($this->settings['bundle']) {
// Add the bundle to all variables of the entity type.
foreach ($variables as $name => $variable_info) {
if ($variable_info['type'] == $this->entityType) {
$variables[$name]['bundle'] = $this->settings['bundle'];
}
}
}
return $variables;
}
}