DefaultsTestPluginManager.php 1.73 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\plugin_test\Plugin;

use Drupal\Component\Plugin\Discovery\StaticDiscovery;
use Drupal\Component\Plugin\Factory\DefaultFactory;
7
use Drupal\Core\Extension\ModuleHandlerInterface;
8
use Drupal\Core\Plugin\DefaultPluginManager;
9 10 11 12

/**
 * Defines a plugin manager used by Plugin API unit tests.
 */
13
class DefaultsTestPluginManager extends DefaultPluginManager {
14

15 16 17 18 19 20
  /**
   * Constructs a new DefaultsTestPluginManager instance.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
21
  public function __construct(ModuleHandlerInterface $module_handler) {
22 23 24 25 26
    // Create the object that can be used to return definitions for all the
    // plugins available for this type. Most real plugin managers use a richer
    // discovery implementation, but StaticDiscovery lets us add some simple
    // mock plugins for unit testing.
    $this->discovery = new StaticDiscovery();
27
    $this->factory = new DefaultFactory($this, 'Drupal\Component\Plugin\PluginInspectionInterface');
28
    $this->moduleHandler = $module_handler;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

    // Specify default values.
    $this->defaults = array(
      'metadata' => array(
        'default' => TRUE,
      ),
    );

    // Add a plugin with a custom value.
    $this->discovery->setDefinition('test_block1', array(
      'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock',
      'metadata' => array(
        'custom' => TRUE,
      ),
    ));
    // Add a plugin that overrides the default value.
    $this->discovery->setDefinition('test_block2', array(
      'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock',
      'metadata' => array(
        'custom' => TRUE,
        'default' => FALSE,
      ),
    ));
  }

}