class NodeType

Provides a 'Node Type' condition.

Plugin annotation


@Plugin(
  id = "node_type",
  label = @Translation("Node Bundle"),
  module = "node",
  context = {
    "node" = {
      "type" = "entity",
      "constraints" = {
        "EntityType" = "node"
      }
    }
  }
)

Hierarchy

Expanded class hierarchy of NodeType

File

drupal/core/modules/node/lib/Drupal/node/Plugin/Condition/NodeType.php, line 31
Contains \Drupal\node\Plugin\Condition\NodeType.

Namespace

Drupal\node\Plugin\Condition
View source
class NodeType extends ConditionPluginBase {

  /**
   * Implements \Drupal\Core\Form\FormInterface::buildForm().
   */
  public function buildForm(array $form, array &$form_state) {
    $form = parent::buildForm($form, $form_state);
    $options = array();
    foreach (node_type_get_types() as $type) {
      $options[$type->type] = $type->name;
    }
    $form['bundles'] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#required' => TRUE,
      '#default_value' => isset($this->configuration['bundles']) ? $this->configuration['bundles'] : array(),
    );
    return $form;
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::validateForm().
   */
  public function validateForm(array &$form, array &$form_state) {
    foreach ($form_state['values']['bundles'] as $bundle) {
      if (!in_array($bundle, array_keys(node_type_get_types()))) {
        form_set_error('bundles', t('You have chosen an invalid node bundle, please check your selection and try again.'));
      }
    }
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::submitForm().
   */
  public function submitForm(array &$form, array &$form_state) {
    $this->configuration['bundles'] = $form_state['values']['bundles'];
    parent::submitForm($form, $form_state);
  }

  /**
   * Implements \Drupal\Core\Executable\ExecutableInterface::summary().
   */
  public function summary() {
    if (count($this->configuration['bundles']) > 1) {
      $bundles = $this->configuration['bundles'];
      $last = array_pop($bundles);
      $bundles = implode(', ', $bundles);
      return t('The node bundle is @bundles or @last', array(
        '@bundles' => $bundles,
        '@last' => $last,
      ));
    }
    $bundle = $this->configuration['bundles'][0];
    return t('The node bundle is @bundle', array(
      '@bundle' => $bundle,
    ));
  }

  /**
   * Implements \Drupal\condition\ConditionInterface::evaluate().
   */
  public function evaluate() {
    $node = $this
      ->getContextValue('node');
    return in_array($node->type, $this->configuration['bundles']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConditionPluginBase::execute public function Implements \Drupal\Core\Executable\ExecutablePluginBase::execute(). Overrides ExecutableInterface::execute
ConditionPluginBase::getFormID public function Implements \Drupal\Core\Form\FormInterface::getFormID(). Overrides FormInterface::getFormID
ConditionPluginBase::isNegated public function Implements \Drupal\condition\Plugin\ConditionInterface::isNegated(). Overrides ConditionInterface::isNegated
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::getContext public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContext(). Overrides ContextAwarePluginInterface::getContext
ContextAwarePluginBase::getContextDefinition public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextDefinition(). Overrides ContextAwarePluginInterface::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextDefinitions(). Overrides ContextAwarePluginInterface::getContextDefinitions
ContextAwarePluginBase::getContexts public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContexts(). Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValue(). Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::getContextValues(). Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContextValue public function Override of \Drupal\Component\Plugin\ContextAwarePluginBase::setContextValue(). Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::valdidateContexts(). Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__construct public function Override of \Drupal\Component\Plugin\ContextAwarePluginBase::__construct(). Overrides ContextAwarePluginBase::__construct
ExecutablePluginBase::$executableManager protected property The condition manager to proxy execute calls through.
ExecutablePluginBase::getConfig public function Gets all configuration values.
ExecutablePluginBase::getConfigDefinition public function Gets the definition of a configuration option.
ExecutablePluginBase::getConfigDefinitions public function Gets an array of definitions of available configuration options.
ExecutablePluginBase::setConfig public function Sets the value of a particular configuration option.
ExecutablePluginBase::setExecutableManager public function
NodeType::buildForm public function Implements \Drupal\Core\Form\FormInterface::buildForm(). Overrides ConditionPluginBase::buildForm
NodeType::evaluate public function Implements \Drupal\condition\ConditionInterface::evaluate(). Overrides ConditionInterface::evaluate
NodeType::submitForm public function Implements \Drupal\Core\Form\FormInterface::submitForm(). Overrides ConditionPluginBase::submitForm
NodeType::summary public function Implements \Drupal\Core\Executable\ExecutableInterface::summary(). Overrides ConditionInterface::summary
NodeType::validateForm public function Implements \Drupal\Core\Form\FormInterface::validateForm(). Overrides ConditionPluginBase::validateForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId