class RulesEventHandlerEntityBundle

Exposes the bundle of an entity as event setting.

Hierarchy

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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
RulesEventHandlerBase::$eventInfo protected property The event info.
RulesEventHandlerBase::$eventName protected property The event name.
RulesEventHandlerBase::$settings protected property The event settings.
RulesEventHandlerBase::getEventInfo public function Implements RulesEventHandlerInterface::getEventInfo(). Overrides RulesEventHandlerInterface::getEventInfo
RulesEventHandlerBase::getEventName public function Implements RulesEventHandlerInterface::getEventName(). Overrides RulesEventHandlerInterface::getEventName
RulesEventHandlerBase::getSettings public function Implements RulesEventHandlerInterface::getSettings(). Overrides RulesEventHandlerInterface::getSettings 1
RulesEventHandlerBase::setSettings public function Implements RulesEventHandlerInterface::setSettings(). Overrides RulesEventHandlerInterface::setSettings
RulesEventHandlerEntityBundle::$bundleKey protected property
RulesEventHandlerEntityBundle::$entityInfo protected property
RulesEventHandlerEntityBundle::$entityType protected property
RulesEventHandlerEntityBundle::availableVariables public function Implements RulesEventHandlerInterface::availableVariables(). Overrides RulesEventHandlerBase::availableVariables
RulesEventHandlerEntityBundle::buildForm public function Implements RulesEventHandlerInterface::buildForm(). Overrides RulesEventHandlerInterface::buildForm
RulesEventHandlerEntityBundle::extractFormValues public function Implements RulesEventHandlerInterface::extractFormValues(). Overrides RulesEventHandlerBase::extractFormValues
RulesEventHandlerEntityBundle::getBundlePropertyLabel protected function Returns the label to use for the bundle property. 3
RulesEventHandlerEntityBundle::getDefaults public function Implements RulesEventHandlerInterface::getDefaults(). Overrides RulesEventHandlerInterface::getDefaults
RulesEventHandlerEntityBundle::getEventNameSuffix public function Implements RulesEventHandlerInterface::getConfiguredEventName(). Overrides RulesEventHandlerInterface::getEventNameSuffix
RulesEventHandlerEntityBundle::summary public function Implements RulesEventHandlerInterface::summary(). Overrides RulesEventHandlerInterface::summary
RulesEventHandlerEntityBundle::validate public function Implements RulesEventHandlerInterface::validate(). Overrides RulesEventHandlerBase::validate
RulesEventHandlerEntityBundle::__construct public function Implements RulesEventHandlerInterface::__construct(). Overrides RulesEventHandlerBase::__construct