Skip to content

Instantly share code, notes, and snippets.

@ForestMars
Forked from EclipseGc/gist:4596068
Created January 28, 2013 04:53
Show Gist options
  • Save ForestMars/4653159 to your computer and use it in GitHub Desktop.
Save ForestMars/4653159 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\node\Plugin\Core\Condition\NodeType.
*/
namespace Drupal\node\Plugin\Core\Condition;
use Drupal\condition\Plugin\ConditionPluginBase;
use Drupal\Core\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Provides a 'Node Type' condition.
*
* @Plugin(
* id = "node_type",
* label = @Translation("Node Bundle"),
* module = "node",
* context = {
* "node" = "Drupal\node\Plugin\Core\Entity\Node"
* }
* )
*/
class NodeType extends ConditionPluginBase {
/**
* Implements Drupal\condition\Plugin\ConditionInterface::form().
*/
public function form($form, &$form_state) {
$options = array();
foreach (node_type_get_types() as $type) {
$options[$type->type] = $type->name;
}
$form['bundles'] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => isset($configuration['bundles']) ? $configuration['bundles'] : array(),
);
return $form;
}
/**
* Implements Drupal\condition\Plugin\ConditionInterface::formValidate().
*/
public function formValidate($form, &$form_state) {
}
/**
* Implements Drupal\condition\Plugin\ConditionInterface::formSubmit().
*/
public function formSubmit($form, &$form_state) {
}
/**
* Implements Drupal\condition\Plugin\ConditionInterface::summary().
*/
public function summary() {
$config = $this->getConfig();
if (count($config['bundles']) > 1) {
$bundles = $config['bundles'];
$last = array_pop($bundles);
$bundles = implode(', ', $bundles);
return t('The node bundle is @bundles or @last', array('@bundles' => $bundles, '@last' => $last));
}
$bundle = $config['bundles'][0];
return t('The node bundle is @bundle', array('@bundle' => $bundle));
}
/**
* Implements Drupal\Core\ExecutableInterface::execute().
*/
public function execute() {
$node = $this->getContext('node');
return in_array($node->type, $this->configuration['bundles']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment