5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Plugin\DefaultPluginManager;
12 * Plugin type manager for field diff builders.
14 * @ingroup diff_layout_builder
16 class DiffLayoutManager extends DefaultPluginManager {
19 * The entity type manager.
21 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
23 protected $entityTypeManager;
26 * Wrapper object for simple configuration from diff.settings.yml.
28 * @var \Drupal\Core\Config\ImmutableConfig
33 * Wrapper object for simple configuration from diff.plugins.yml.
35 * @var \Drupal\Core\Config\ImmutableConfig
37 protected $layoutPluginsConfig;
40 * Constructs a DiffLayoutManager object.
42 * @param \Traversable $namespaces
43 * An object that implements \Traversable which contains the root paths
44 * keyed by the corresponding namespace to look for plugin implementations.
45 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
46 * Cache backend instance to use.
47 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
49 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
50 * The entity type manager.
51 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
52 * The configuration factory.
54 public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
55 parent::__construct('Plugin/diff/Layout', $namespaces, $module_handler, '\Drupal\diff\DiffLayoutInterface', 'Drupal\diff\Annotation\DiffLayoutBuilder');
57 $this->setCacheBackend($cache_backend, 'diff_layout_builder_plugins');
58 $this->alterInfo('diff_layout_builder_info');
59 $this->entityTypeManager = $entity_type_manager;
60 $this->config = $config_factory->get('diff.settings');
61 $this->layoutPluginsConfig = $config_factory->get('diff.layout_plugins');
65 * Gets the applicable layout plugins.
67 * Loop over the plugins that can be used to display the diff comparison
68 * sorting them by the weight.
71 * The layout plugin options.
73 public function getPluginOptions() {
74 $plugins = $this->config->get('general_settings.layout_plugins');
76 // Get the plugins sorted and build an array keyed by the plugin id.
78 // Sort the plugins based on their weight.
79 uasort($plugins, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
80 foreach ($plugins as $key => $value) {
81 if ($this->hasDefinition($key)) {
82 $plugin = $this->getDefinition($key);
83 if ($plugin && $value['enabled']) {
84 $plugin_options[$key] = $plugin['label'];
89 return $plugin_options;
93 * Gets the default layout plugin selected.
95 * Take the first option of the array returned by getPluginOptions.
98 * The id of the default plugin.
100 public function getDefaultLayout() {
101 $plugins = array_keys($this->getPluginOptions());
102 return reset($plugins);
108 public function findDefinitions() {
109 $definitions = parent::findDefinitions();
111 // Remove plugin html_diff if library is not present.
112 $has_htmlDiffAdvanced = class_exists('\HtmlDiffAdvanced');
113 if (!$has_htmlDiffAdvanced) {
114 unset($definitions['visual_inline']);