10.3 基于注解的插件

Drupal8龡龞龝龜龚龗龔龐龎龌龉龅龌龁龀齾齺齷齶齴齰

1齪齧齥齡齝

齜齘core/modules/user/src/Plugin/Validation/Constraint/UserNameUnique.php龡鼬鼩鼧鼣齜鼠鼞鼛鼗

/**
 * Checks if a user name is unique on the site.
 *
 * @Constraint(
 *   id = "UserNameUnique",
 *   label = @Translation("User name unique", context = "Validation"),
 * )
 */
class UserNameUnique extends Constraint {
...
}

黩黨黧黤黣龚龗Constraint龔龐龎龀齾黟黜黚龗默黕鼗

  • id 龔龐ID
  • label 黑黐

2齪黍黉黆龞黄黂龀齾

黁龞龅黍黣麽麺Drupal齴麷麴黄黂龎齘Drupal8麳麰龎麯麭龚麫龅齘麨龗module麧龐龡黧黤龎PHP麢麠齴麝麚麖麓麐麏麧龐麢麠麎麷麴龎黩麋麈麆麄麂麁鼠齰鹽龌龁龀齾龎鹼鹻龞麓龀齾鹸黕麢麠麎麷麴龎鹵鹽黄麷麴黄黂鹲鹯麺齰

3齪龀齾齴鹫鹧

龀齾黄黂鹤黧齴鹢鹡齺鹟鹛鹚鹙麺鹖鹓龎鹐鹌鹈鹄鹀齰

龀齾鹫鹧鸼鼗 http://doctrine-orm.readthedocs.io/en/latest/reference/annotations-reference.html?highlight=annotations

  • id: 龔龐齴鸯鸫鸩龎鸥龅鸡龚ID
  • 鸝鸚黄黂鸙鸘鸔龎鸐龞黄黂齥鸘鸔龎齥鸘鸔麖鸏鸌鸊龉
  • 龉黂齴鹖鹓鸆鸅鼗
    • String: 默鸀鷾鸆鸅鸝鸚黄黂鸙鸘鸔齝齜"foo"齰齜鷼默鸀鷾龡黟黜鸙鸘鸔鷸鹻鷵黄黂龚鷱鸙鸘鸔鼩鷰黤龎齜 "The ""On"" Value"齰
    • Numbers: 鸐鷩黄黂鸘鸔齝齜21龎黄黂鸘鸔鷦鷢齾鷞齶默鸀鷾齰
    • Booleans: 鸐鷩黄黂鸘鸔齝齜TRUE鷔鷒FALSE龎黄黂鸘鸔鷦鷢齾鷞齶默鸀鷾齰
    • Lists: 鷋鷉鹖鷆龎齜:

      base = { "node", "foo", } 鷃龀鷀鷋鷉龡齴鹲鶾龚龗鶽鶻鶾齴鶷鸔龎鶶麏鶵鶱鶰鶬齾鷞鶨鶥齰

    • Maps: 鹖鹓鶠鶞龎鶛齜鹖鷆齰齜:

      edit = { "editor" = "direct"龎 }

    • 鶘鶔黄黂龉鶓

4齪鶎黧黤龀齾鸆

齘 core/modules/text/src/Plugin/Field/FieldFormatter/TextTrimmedFormatter.php 龡鼬鼩鶊鼣黄黂黣龚龗鶎黧黤齴龀齾鸆FieldFormatter齰

<?php

namespace Drupal\text\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'text_trimmed' formatter.
 *
 * Note: This class also contains the implementations used by the
 * 'text_summary_or_trimmed' formatter.
 *
 * @see \Drupal\text\Field\Formatter\TextSummaryOrTrimmedFormatter
 *
 * @FieldFormatter(
 *   id = "text_trimmed",
 *   label = @Translation("Trimmed"),
 *   field_types = {
 *     "text",
 *     "text_long",
 *     "text_with_summary"
 *   },
 *   quickedit = {
 *     "editor" = "form"
 *   }
 * )
 */
class TextTrimmedFormatter extends FormatterBase {
...
}

5齪齘龔龐鸆鸅龡黄黂龀齾

齜鵯齘龔龐鸆鸅龡黄黂龀齾鵭鵩鹼龞鵥鵢DefaultPlguinManager鹐黄黂AnnotatedClassDiscovery.

DefaultPluginManager鹙鵛鵙鹖齴鵕龚龗鵓鹖龅鵐黧龔龐齴鵎鵋鵉鵇龎鹲鶾龚龗鵓鹖龅鵄鵃黧黤齴鶎黧黤龀鵂鸆齰齘黩龗齝鵀龡龎鷦麖齘hello_world/src/Plugin/Field/FieldFormatter鴾鴼龡鵎鵋龔龐齰

use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;

class FormatterPluginManager extends DefaultPluginManager {

  /**
   * Constructs a FormatterPluginManager object.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager) {
    parent::__construct('Plugin/Field/FieldFormatter', $namespaces, $module_handler, 'Drupal\Core\Field\FormatterInterface', 'Drupal\Core\Field\Annotation\FieldFormatter');

    $this->setCacheBackend($cache_backend, 'field_formatter_types_plugins');
    $this->alterInfo('field_formatter_info');
    $this->fieldTypeManager = $field_type_manager;
  }
}

鹽龀鴲齴鴯鸩鴮鴭齺鶎鶱鴫鴩龀鴲鴨鸫龎齜FieldBundle:

/**
 * @file
 * Contains Drupal\field\FieldBundle.
 */

namespace Drupal\field;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
 * Field dependency injection container.
 */
class FieldBundle extends Bundle {

  /**
   * Overrides Symfony\Component\HttpKernel\Bundle\Bundle::build().
   */
  public function build(ContainerBuilder $container) {
    // Register the plugin managers for our plugin types with the dependency injection container.
    $container->register('plugin.manager.field.widget', 'Drupal\field\Plugin\Type\Widget\WidgetPluginManager')
      ->addArgument('%container.namespaces%');
    $container->register('plugin.manager.field.formatter', 'Drupal\field\Plugin\Type\Formatter\FormatterPluginManager')
      ->addArgument('%container.namespaces%');
  }

}

6齪齜鵯鴛黂鶎黧黤齴龔龐

鸝鸚齘鸆齴鹙鵛鵙鹖龡龀鴲鴯鸩鴮鴭鼗

$type = new CustomPluginManager(\Drupal::getContainer()->getParameter('container.namespaces'));

本书共79小节:

评论 (写第一个评论)