diff --git a/src/Annotation/Layout.php b/src/Annotation/Layout.php index 2d9faaf..96f006e 100644 --- a/src/Annotation/Layout.php +++ b/src/Annotation/Layout.php @@ -25,6 +25,7 @@ use Drupal\Component\Annotation\Plugin; * @Annotation */ class Layout extends Plugin { + /** * The plugin ID. * @@ -47,7 +48,7 @@ class Layout extends Plugin { /** * The human-readable name. * - * @war \Drupal\Core\Annotation\Translation + * @var \Drupal\Core\Annotation\Translation * * @ingroup plugin_translatable */ @@ -103,8 +104,7 @@ class Layout extends Plugin { public $template; /** - * Base path (relative to current module or theme) to all resources (like the - * icon). + * Path (relative to the module or theme) to resources like icon or template. * * @var string optional */ @@ -136,7 +136,7 @@ class Layout extends Plugin { public $css; /** - * The path to the preview image (relative to the base path). + * The path to the preview image (relative to the 'path' given). * * @var string optional */ diff --git a/src/Plugin/Layout/LayoutBase.php b/src/Plugin/Layout/LayoutBase.php index 8d85135..7cdabf7 100644 --- a/src/Plugin/Layout/LayoutBase.php +++ b/src/Plugin/Layout/LayoutBase.php @@ -11,7 +11,6 @@ use Drupal\Component\Plugin\ConfigurablePluginInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\PluginBase; use Drupal\Core\Plugin\PluginFormInterface; -use Drupal\layout_plugin\Layout; /** * Provides a base class for Layout plugins. @@ -19,25 +18,46 @@ use Drupal\layout_plugin\Layout; abstract class LayoutBase extends PluginBase implements LayoutInterface, ConfigurablePluginInterface, PluginFormInterface { /** - * @var array * The layout configuration. + * + * @var array */ protected $configuration = []; /** - * Get the plugin's description. + * Gets the human-readable name. * - * @return string - * The layout description + * @return \Drupal\Core\Annotation\Translation|NULL + * The human-readable name. + */ + public function getLabel() { + return $this->pluginDefinition['label']; + } + + /** + * Gets the optional description for advanced layouts. + * + * @return \Drupal\Core\Annotation\Translation|NULL + * The layout description. */ public function getDescription() { - return $this->pluginDefinition['description']; + return isset($this->pluginDefinition['description']) ? $this->pluginDefinition['description'] : NULL; } /** - * Get human-readable list of regions keyed by machine name. + * Gets the human-readable category. * - * @return array + * @return \Drupal\Core\Annotation\Translation + * The human-readable category. + */ + public function getCategory() { + return $this->pluginDefinition['category']; + } + + /** + * Gets human-readable list of regions keyed by machine name. + * + * @return \Drupal\Core\Annotation\Translation[] * An array of human-readable region names keyed by machine name. */ public function getRegionNames() { @@ -45,7 +65,7 @@ abstract class LayoutBase extends PluginBase implements LayoutInterface, Configu } /** - * Get information on regions keyed by machine name. + * Gets information on regions keyed by machine name. * * @return array * An array of information on regions keyed by machine name. @@ -55,46 +75,46 @@ abstract class LayoutBase extends PluginBase implements LayoutInterface, Configu } /** - * Get the base path for all resources. + * Gets the path to resources like icon or template. * - * @return string - * The full base to all resources. + * @return string|NULL + * The path relative to the Drupal root. */ public function getBasePath() { - return isset($this->pluginDefinition['path']) && $this->pluginDefinition['path'] ? $this->pluginDefinition['path'] : FALSE; + return isset($this->pluginDefinition['path']) ? $this->pluginDefinition['path'] : NULL; } /** - * Get the full path to the icon image. + * Gets the path to the preview image. * * This can optionally be used in the user interface to show the layout of * regions visually. * - * @return string - * The full path to preview image file. + * @return string|NULL + * The path to preview image file. */ public function getIconFilename() { - return isset($this->pluginDefinition['icon']) && $this->pluginDefinition['icon'] ? $this->pluginDefinition['icon'] : FALSE; + return isset($this->pluginDefinition['icon']) ? $this->pluginDefinition['icon'] : NULL; } /** - * Get the asset library name. + * Get the asset library. * - * @return string + * @return string|NULL * The asset library. */ public function getLibrary() { - return isset($this->pluginDefinition['library']) && $this->pluginDefinition['library'] ? $this->pluginDefinition['library'] : FALSE; + return isset($this->pluginDefinition['library']) ? $this->pluginDefinition['library'] : NULL; } /** - * Get the theme function for rendering this layout. + * Gets the theme hook used to render this layout. * - * @return string - * Theme function name. + * @return string|NULL + * Theme hook. */ - public function getTheme() { - return isset($this->pluginDefinition['theme']) && $this->pluginDefinition['theme'] ? $this->pluginDefinition['theme'] : FALSE; + public function getThemeHook() { + return isset($this->pluginDefinition['theme']) ? $this->pluginDefinition['theme'] : NULL; } /** @@ -104,7 +124,7 @@ abstract class LayoutBase extends PluginBase implements LayoutInterface, Configu $build = $regions; $build['#layout'] = $this->getPluginDefinition(); $build['#settings'] = $this->getConfiguration(); - if ($theme = $this->getTheme()) { + if ($theme = $this->getThemeHook()) { $build['#theme'] = $theme; } if ($library = $this->getLibrary()) { @@ -155,9 +175,10 @@ abstract class LayoutBase extends PluginBase implements LayoutInterface, Configu } /** - * @{inheritdoc} + * {@inheritdoc} */ public function calculateDependencies() { - return []; + return isset($this->configuration['dependencies']) ? $this->configuration['dependencies'] : []; } + } diff --git a/src/Plugin/Layout/LayoutPluginManager.php b/src/Plugin/Layout/LayoutPluginManager.php index af510ed..c1658ff 100644 --- a/src/Plugin/Layout/LayoutPluginManager.php +++ b/src/Plugin/Layout/LayoutPluginManager.php @@ -87,6 +87,17 @@ class LayoutPluginManager extends DefaultPluginManager implements LayoutPluginMa } $definition['path'] = !empty($definition['path']) ? $base_path . '/' . $definition['path'] : $base_path; + // Add a dependency on the provider of the library. + if (!empty($definition['library'])) { + list ($library_provider, ) = explode('/', $definition['library']); + if ($this->moduleHandler->moduleExists($library_provider)) { + $definition['dependencies'] = ['module' => [$library_provider]]; + } + elseif ($this->themeHandler->themeExists($library_provider)) { + $definition['dependencies'] = ['theme' => [$library_provider]]; + } + } + // Add the path to the icon filename. if (!empty($definition['icon'])) { $definition['icon'] = $definition['path'] . '/' . $definition['icon']; diff --git a/tests/src/Unit/PluginManagerTest.php b/tests/src/Unit/PluginManagerTest.php index 02b4d07..c74cc9a 100644 --- a/tests/src/Unit/PluginManagerTest.php +++ b/tests/src/Unit/PluginManagerTest.php @@ -67,6 +67,7 @@ class PluginManagerTest extends UnitTestCase { 'label' => 'Complex layout', 'category' => 'Test layouts', 'template' => 'complex-layout', + 'library' => 'library_module/library_name', 'provider' => 'layout_plugin_test', 'path' => 'layout/complex', 'icon' => 'complex-layout.png', @@ -80,6 +81,7 @@ class PluginManagerTest extends UnitTestCase { $this->assertEquals('modules/layout_plugin_test/layout/complex', $definition['template_path']); $this->assertEquals('modules/layout_plugin_test/layout/complex/complex-layout.png', $definition['icon']); $this->assertEquals('complex_layout', $definition['theme']); + $this->assertEquals(['module' => ['library_module']], $definition['dependencies']); // A layout with a template path. $definition = [