View source
<?php
namespace Drupal\node\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NodeType extends ConditionPluginBase implements ContainerFactoryPluginInterface {
protected $entityStorage;
public function __construct(EntityStorageInterface $entity_storage, array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
@trigger_error('\\Drupal\\node\\Plugin\\Condition\\NodeType is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \\Drupal\\Core\\Entity\\Plugin\\Condition\\EntityBundle instead. See https://www.drupal.org/node/2983299', E_USER_DEPRECATED);
$this->entityStorage = $entity_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('entity_type.manager')
->getStorage('node_type'), $configuration, $plugin_id, $plugin_definition);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$options = [];
$node_types = $this->entityStorage
->loadMultiple();
foreach ($node_types as $type) {
$options[$type
->id()] = $type
->label();
}
$form['bundles'] = [
'#title' => $this
->t('Node types'),
'#type' => 'checkboxes',
'#options' => $options,
'#default_value' => $this->configuration['bundles'],
];
return parent::buildConfigurationForm($form, $form_state);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['bundles'] = array_filter($form_state
->getValue('bundles'));
parent::submitConfigurationForm($form, $form_state);
}
public function summary() {
if (count($this->configuration['bundles']) > 1) {
$bundles = $this->configuration['bundles'];
$last = array_pop($bundles);
$bundles = implode(', ', $bundles);
return $this
->t('The node bundle is @bundles or @last', [
'@bundles' => $bundles,
'@last' => $last,
]);
}
$bundle = reset($this->configuration['bundles']);
return $this
->t('The node bundle is @bundle', [
'@bundle' => $bundle,
]);
}
public function evaluate() {
if (empty($this->configuration['bundles']) && !$this
->isNegated()) {
return TRUE;
}
$node = $this
->getContextValue('node');
return !empty($this->configuration['bundles'][$node
->getType()]);
}
public function defaultConfiguration() {
return [
'bundles' => [],
] + parent::defaultConfiguration();
}
}