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小节:

评论 (写第一个评论)