10.6 插件上下文

1龶龲龰龮龪龧龤龡

"龪龧龤Context"龋龈龆龮龲龰龅龄龂龡龁齽齼齹齸齴齽齰齯齫齧龶龤齥齣齢齞齝齜龅龄龮齙齖齣齢齕齯齑龈齐齏齎齍齋龧齜齊齉齖齝齎齈齄齣齢齃鼿龅鼻鼷

鼳鼯龋鼫鼪:"鼥鼣鼡鼞鼜drupal"齜鼯鼇鼅鼄鼃龮鼁鼿龅鼻齎齜鼀黾黺黷黳黲"鼥"黱黭黫齜鼡齣齢齸齖齝齑齏齏鼻黧黥黤"鼥"龮黫齜龋龈"鼥"黠龮龪龧龤齜龋龈黫黠龮龪龧龤齎黝鼷

鼡黛黚黙默齸齜龪龧龤龮齑黗黔黐齎鼿黍黍黉齜齴黇黾黃黂鼡齍齋齈齎麿麽齏鼻齍齋鼷黷麻黺麷麳麯麫麩龋麨晦麤齎齏鼻齜麣麳麠黳黲"龪龧龤"麝齙齯"齍齋"黠麛黤齜齴黇龮麚麘齎鼷

麔麓麐麎麌麊麇麃麀鼣齎鹽鹼鹺麌鹹鹸鹼齜鹶龰鹵齴鹲鹱鹭鹫麩齸齜鹧齹鹶齑鹥齜鼥鹤齑黚鹢鹟黠龮鹞鹚齍齋齜鹖鼫鼪鼃黠龮鹔黧黥鹒鹑鹎鼡齎龪龧龤齜鹋鹉鼥齃麌鼿鹎麛鹇鼷

鹃齰龪龧龤鹁鸽龮鼯鸼鸸麠齜鼡鸵鸲齞齝鸱鸮齑龈鸫麽鸩鼿鸧齎齎麛黾龮鸱鸮鼡齍齋鸣龪齎齜鸟鹢鸟鸝鸚龮鼯鸼.

鼡Drupal 8齸龪龧龤黠龮黱齙鸑鸏鹭麎鸌齜鸉鸅鹎齕齎黙鸂齍齋齜龋龈齍齋鸁鸑鸏鷾鸉鸅麓鷺鷷鷴鷲鷰齜鸉鸅鷯鷫麠鹔黧黥齍齋鷧龪龧龤鷦齃黳黲鹒鹑鷢鷠龰麛鹇鷧鼣鷞齖龤鹎鷜鷦鼷

鼳鼯鷙鷘龪龧龤CacheContext齜黠龮黱麝麿齯鷙鷘鸉鸅齎齍齋鷧鷙鷘齍齋龮鸉鸅齍齋齎鸲鷑鷦齜鷙鷘鸉鸅齊鷍鸼齍齋齃麌鼣鷊麛鹇齜鷆鷄鷂鷁黠龮鷙鷘齊鷍龋龈龪龧龤麎鷘鹱鶿鶼鶻鼣鷊齎鶷鷍鼷

2龶鶲黚龪龧龤

鶱齽齜鶲黚龪龧龤鶮黇鹱鼡鸧鶪src/Plugin/Condition龧鼷

龧齝鶟鸲鶮黇鷂鷁黤齑龈NodeType鹽齜齴齎龪龧龤龮node鷂鷄齜麣鼿鼡node鷂鷄鶘鶖鶔齎鹟鶓龧齴齃齼鶑鼷

core/modules/node/src/Plugin/Condition/NodeType.php黭鶎鼯龧鸩

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

/**
 * Provides a 'Node Type' condition.
 *
 * @Condition(
 *   id = "node_type",
 *   label = @Translation("Node Bundle"),
 *   context = {
 *     "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
 *   }
 * )
 */
class NodeType extends ConditionPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The entity storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $entityStorage;

  /**
   * Creates a new NodeType instance.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
   *   The entity storage.
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name. The special key 'context' may be used to
   *   initialize the defined contexts by setting it to an array of context
   *   values keyed by context names.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   */
  public function __construct(EntityStorageInterface $entity_storage, array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityStorage = $entity_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $container->get('entity.manager')->getStorage('node_type'),
      $configuration,
      $plugin_id,
      $plugin_definition
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $options = array();
    $node_types = $this->entityStorage->loadMultiple();
    foreach ($node_types as $type) {
      $options[$type->id()] = $type->label();
    }
    $form['bundles'] = array(
      '#title' => $this->t('Node types'),
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => $this->configuration['bundles'],
    );
    return parent::buildConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['bundles'] = array_filter($form_state->getValue('bundles'));
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  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', array('@bundles' => $bundles, '@last' => $last));
    }
    $bundle = reset($this->configuration['bundles']);
    return $this->t('The node bundle is @bundle', array('@bundle' => $bundle));
  }

  /**
   * {@inheritdoc}
   */
  public function evaluate() {
    if (empty($this->configuration['bundles']) && !$this->isNegated()) {
      return TRUE;
    }
    $node = $this->getContextValue('node');
    return !empty($this->configuration['bundles'][$node->getType()]);
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return array('bundles' => array()) + parent::defaultConfiguration();
  }

}

@Condition鴫鴪鴪鴩鸩

  • id: 鶲黚ID
  • label: 鶔鴥鴢齝龪鴡鴟齎鴝鴚
  • context: 鶖鶔evaluate鴘鴕麎鴔鴑node齎鹽鴏

鴋鼥齎齑鴇鴆鸩

  • required: 黱齏龮鴁黾鳽鳺齜鳹鳸黾true
  • multiple: 黱齏龮鴁鼿鳴龈鳰黚齜鳹鳸黾false
  • description: 龪龧龤齎鳯鳫齜鶔齯UI
  • default_value: 鳹鳸黾false
  • class: 鳹鳸\Drupal\Core\Plugin\ContextDefinition鹽鼷鼯鼇黺鳩鳦黺鹒鳤齎鹽齜黺鳽鳺鷂鷁\Drupal\Core\Plugin\Context\ContextDefinitionInterface鳠鳞鼷

鴫龅鸩龪龧龤齏鼻鹃鳝黷鳛鶔default_value鴆鼷

3龶鴋鼥齎齑鴇鴟鶟

鴋鼥齎齑鴇鴟鶟鹃鳝鼡Drupal8鸧鶪龧鳔鳓鹭鸩

jerry@mac:~/Sites/drupal8 > grep "@Condition" * -R
core/modules/block/tests/modules/block_test/src/Plugin/Condition/BaloneySpam.php: * @Condition(
core/modules/block/tests/modules/block_test/src/Plugin/Condition/MissingSchema.php: * @Condition(
core/modules/language/src/Plugin/Condition/Language.php: * @Condition(
core/modules/node/src/Plugin/Condition/NodeType.php: * @Condition(
core/modules/system/src/Plugin/Condition/CurrentThemeCondition.php: * @Condition(
core/modules/system/src/Plugin/Condition/RequestPath.php: * @Condition(
core/modules/system/tests/modules/condition_test/src/Plugin/Condition/ConditionTestDualUser.php: * @Condition(
core/modules/system/tests/modules/condition_test/src/Plugin/Condition/ConditionTestNoExistingType.php: * @Condition(
core/modules/system/tests/modules/condition_test/src/Plugin/Condition/OptionalContextCondition.php: * @Condition(
core/modules/user/src/Plugin/Condition/UserRole.php: * @Condition(
modules/ctools/src/Plugin/Condition/EntityBundle.php: * @Condition(

本书共79小节:

评论 (写第一个评论)