annotate core/lib/Drupal/Core/Layout/LayoutDefinition.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Layout;
Chris@0 4
Chris@0 5 use Drupal\Component\Plugin\Definition\DerivablePluginDefinitionInterface;
Chris@0 6 use Drupal\Component\Plugin\Definition\PluginDefinitionInterface;
Chris@0 7 use Drupal\Component\Plugin\Definition\PluginDefinition;
Chris@0 8 use Drupal\Core\Plugin\Definition\DependentPluginDefinitionInterface;
Chris@0 9 use Drupal\Core\Plugin\Definition\DependentPluginDefinitionTrait;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Provides an implementation of a layout definition and its metadata.
Chris@0 13 */
Chris@0 14 class LayoutDefinition extends PluginDefinition implements PluginDefinitionInterface, DerivablePluginDefinitionInterface, DependentPluginDefinitionInterface {
Chris@0 15
Chris@0 16 use DependentPluginDefinitionTrait;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * The name of the deriver of this layout definition, if any.
Chris@0 20 *
Chris@0 21 * @var string|null
Chris@0 22 */
Chris@0 23 protected $deriver;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The human-readable name.
Chris@0 27 *
Chris@0 28 * @var string
Chris@0 29 */
Chris@0 30 protected $label;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * An optional description for advanced layouts.
Chris@0 34 *
Chris@0 35 * @var string
Chris@0 36 */
Chris@0 37 protected $description;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * The human-readable category.
Chris@0 41 *
Chris@0 42 * @var string
Chris@0 43 */
Chris@0 44 protected $category;
Chris@0 45
Chris@0 46 /**
Chris@0 47 * The template file to render this layout (relative to the 'path' given).
Chris@0 48 *
Chris@0 49 * @var string|null
Chris@0 50 */
Chris@0 51 protected $template;
Chris@0 52
Chris@0 53 /**
Chris@0 54 * The path to the template.
Chris@0 55 *
Chris@0 56 * @var string
Chris@0 57 */
Chris@0 58 protected $templatePath;
Chris@0 59
Chris@0 60 /**
Chris@0 61 * The theme hook used to render this layout.
Chris@0 62 *
Chris@0 63 * @var string|null
Chris@0 64 */
Chris@0 65 protected $theme_hook;
Chris@0 66
Chris@0 67 /**
Chris@0 68 * Path (relative to the module or theme) to resources like icon or template.
Chris@0 69 *
Chris@0 70 * @var string
Chris@0 71 */
Chris@0 72 protected $path;
Chris@0 73
Chris@0 74 /**
Chris@0 75 * The asset library.
Chris@0 76 *
Chris@0 77 * @var string|null
Chris@0 78 */
Chris@0 79 protected $library;
Chris@0 80
Chris@0 81 /**
Chris@0 82 * The path to the preview image.
Chris@0 83 *
Chris@0 84 * @var string
Chris@0 85 */
Chris@0 86 protected $icon;
Chris@0 87
Chris@0 88 /**
Chris@14 89 * An array defining the regions of a layout.
Chris@14 90 *
Chris@14 91 * @var string[][]|null
Chris@14 92 *
Chris@14 93 * @see \Drupal\Core\Layout\Icon\IconBuilderInterface::build()
Chris@14 94 */
Chris@14 95 protected $icon_map;
Chris@14 96
Chris@14 97 /**
Chris@0 98 * An associative array of regions in this layout.
Chris@0 99 *
Chris@0 100 * The key of the array is the machine name of the region, and the value is
Chris@0 101 * an associative array with the following keys:
Chris@0 102 * - label: (string) The human-readable name of the region.
Chris@0 103 *
Chris@0 104 * Any remaining keys may have special meaning for the given layout plugin,
Chris@0 105 * but are undefined here.
Chris@0 106 *
Chris@0 107 * @var array
Chris@0 108 */
Chris@0 109 protected $regions = [];
Chris@0 110
Chris@0 111 /**
Chris@0 112 * The default region.
Chris@0 113 *
Chris@0 114 * @var string
Chris@0 115 */
Chris@0 116 protected $default_region;
Chris@0 117
Chris@0 118 /**
Chris@0 119 * Any additional properties and values.
Chris@0 120 *
Chris@0 121 * @var array
Chris@0 122 */
Chris@0 123 protected $additional = [];
Chris@0 124
Chris@0 125 /**
Chris@0 126 * LayoutDefinition constructor.
Chris@0 127 *
Chris@0 128 * @param array $definition
Chris@0 129 * An array of values from the annotation.
Chris@0 130 */
Chris@0 131 public function __construct(array $definition) {
Chris@0 132 foreach ($definition as $property => $value) {
Chris@0 133 $this->set($property, $value);
Chris@0 134 }
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * Gets any arbitrary property.
Chris@0 139 *
Chris@0 140 * @param string $property
Chris@0 141 * The property to retrieve.
Chris@0 142 *
Chris@0 143 * @return mixed
Chris@0 144 * The value for that property, or NULL if the property does not exist.
Chris@0 145 */
Chris@0 146 public function get($property) {
Chris@0 147 if (property_exists($this, $property)) {
Chris@0 148 $value = isset($this->{$property}) ? $this->{$property} : NULL;
Chris@0 149 }
Chris@0 150 else {
Chris@0 151 $value = isset($this->additional[$property]) ? $this->additional[$property] : NULL;
Chris@0 152 }
Chris@0 153 return $value;
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Sets a value to an arbitrary property.
Chris@0 158 *
Chris@0 159 * @param string $property
Chris@0 160 * The property to use for the value.
Chris@0 161 * @param mixed $value
Chris@0 162 * The value to set.
Chris@0 163 *
Chris@0 164 * @return $this
Chris@0 165 */
Chris@0 166 public function set($property, $value) {
Chris@0 167 if (property_exists($this, $property)) {
Chris@0 168 $this->{$property} = $value;
Chris@0 169 }
Chris@0 170 else {
Chris@0 171 $this->additional[$property] = $value;
Chris@0 172 }
Chris@0 173 return $this;
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * Gets the human-readable name of the layout definition.
Chris@0 178 *
Chris@0 179 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
Chris@0 180 * The human-readable name of the layout definition.
Chris@0 181 */
Chris@0 182 public function getLabel() {
Chris@0 183 return $this->label;
Chris@0 184 }
Chris@0 185
Chris@0 186 /**
Chris@0 187 * Sets the human-readable name of the layout definition.
Chris@0 188 *
Chris@0 189 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $label
Chris@0 190 * The human-readable name of the layout definition.
Chris@0 191 *
Chris@0 192 * @return $this
Chris@0 193 */
Chris@0 194 public function setLabel($label) {
Chris@0 195 $this->label = $label;
Chris@0 196 return $this;
Chris@0 197 }
Chris@0 198
Chris@0 199 /**
Chris@0 200 * Gets the description of the layout definition.
Chris@0 201 *
Chris@0 202 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
Chris@0 203 * The description of the layout definition.
Chris@0 204 */
Chris@0 205 public function getDescription() {
Chris@0 206 return $this->description;
Chris@0 207 }
Chris@0 208
Chris@0 209 /**
Chris@0 210 * Sets the description of the layout definition.
Chris@0 211 *
Chris@0 212 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $description
Chris@0 213 * The description of the layout definition.
Chris@0 214 *
Chris@0 215 * @return $this
Chris@0 216 */
Chris@0 217 public function setDescription($description) {
Chris@0 218 $this->description = $description;
Chris@0 219 return $this;
Chris@0 220 }
Chris@0 221
Chris@0 222 /**
Chris@0 223 * Gets the human-readable category of the layout definition.
Chris@0 224 *
Chris@0 225 * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
Chris@0 226 * The human-readable category of the layout definition.
Chris@0 227 */
Chris@0 228 public function getCategory() {
Chris@0 229 return $this->category;
Chris@0 230 }
Chris@0 231
Chris@0 232 /**
Chris@0 233 * Sets the human-readable category of the layout definition.
Chris@0 234 *
Chris@0 235 * @param string|\Drupal\Core\StringTranslation\TranslatableMarkup $category
Chris@0 236 * The human-readable category of the layout definition.
Chris@0 237 *
Chris@0 238 * @return $this
Chris@0 239 */
Chris@0 240 public function setCategory($category) {
Chris@0 241 $this->category = $category;
Chris@0 242 return $this;
Chris@0 243 }
Chris@0 244
Chris@0 245 /**
Chris@0 246 * Gets the template name.
Chris@0 247 *
Chris@0 248 * @return string|null
Chris@0 249 * The template name, if it exists.
Chris@0 250 */
Chris@0 251 public function getTemplate() {
Chris@0 252 return $this->template;
Chris@0 253 }
Chris@0 254
Chris@0 255 /**
Chris@0 256 * Sets the template name.
Chris@0 257 *
Chris@0 258 * @param string|null $template
Chris@0 259 * The template name.
Chris@0 260 *
Chris@0 261 * @return $this
Chris@0 262 */
Chris@0 263 public function setTemplate($template) {
Chris@0 264 $this->template = $template;
Chris@0 265 return $this;
Chris@0 266 }
Chris@0 267
Chris@0 268 /**
Chris@0 269 * Gets the template path.
Chris@0 270 *
Chris@0 271 * @return string
Chris@0 272 * The template path.
Chris@0 273 */
Chris@0 274 public function getTemplatePath() {
Chris@0 275 return $this->templatePath;
Chris@0 276 }
Chris@0 277
Chris@0 278 /**
Chris@0 279 * Sets the template path.
Chris@0 280 *
Chris@0 281 * @param string $template_path
Chris@0 282 * The template path.
Chris@0 283 *
Chris@0 284 * @return $this
Chris@0 285 */
Chris@0 286 public function setTemplatePath($template_path) {
Chris@0 287 $this->templatePath = $template_path;
Chris@0 288 return $this;
Chris@0 289 }
Chris@0 290
Chris@0 291 /**
Chris@0 292 * Gets the theme hook.
Chris@0 293 *
Chris@0 294 * @return string|null
Chris@0 295 * The theme hook, if it exists.
Chris@0 296 */
Chris@0 297 public function getThemeHook() {
Chris@0 298 return $this->theme_hook;
Chris@0 299 }
Chris@0 300
Chris@0 301 /**
Chris@0 302 * Sets the theme hook.
Chris@0 303 *
Chris@0 304 * @param string $theme_hook
Chris@0 305 * The theme hook.
Chris@0 306 *
Chris@0 307 * @return $this
Chris@0 308 */
Chris@0 309 public function setThemeHook($theme_hook) {
Chris@0 310 $this->theme_hook = $theme_hook;
Chris@0 311 return $this;
Chris@0 312 }
Chris@0 313
Chris@0 314 /**
Chris@0 315 * Gets the base path for this layout definition.
Chris@0 316 *
Chris@0 317 * @return string
Chris@0 318 * The base path.
Chris@0 319 */
Chris@0 320 public function getPath() {
Chris@0 321 return $this->path;
Chris@0 322 }
Chris@0 323
Chris@0 324 /**
Chris@0 325 * Sets the base path for this layout definition.
Chris@0 326 *
Chris@0 327 * @param string $path
Chris@0 328 * The base path.
Chris@0 329 *
Chris@0 330 * @return $this
Chris@0 331 */
Chris@0 332 public function setPath($path) {
Chris@0 333 $this->path = $path;
Chris@0 334 return $this;
Chris@0 335 }
Chris@0 336
Chris@0 337 /**
Chris@0 338 * Gets the asset library for this layout definition.
Chris@0 339 *
Chris@0 340 * @return string|null
Chris@0 341 * The asset library, if it exists.
Chris@0 342 */
Chris@0 343 public function getLibrary() {
Chris@0 344 return $this->library;
Chris@0 345 }
Chris@0 346
Chris@0 347 /**
Chris@0 348 * Sets the asset library for this layout definition.
Chris@0 349 *
Chris@0 350 * @param string|null $library
Chris@0 351 * The asset library.
Chris@0 352 *
Chris@0 353 * @return $this
Chris@0 354 */
Chris@0 355 public function setLibrary($library) {
Chris@0 356 $this->library = $library;
Chris@0 357 return $this;
Chris@0 358 }
Chris@0 359
Chris@0 360 /**
Chris@0 361 * Gets the icon path for this layout definition.
Chris@0 362 *
Chris@0 363 * @return string|null
Chris@0 364 * The icon path, if it exists.
Chris@0 365 */
Chris@0 366 public function getIconPath() {
Chris@0 367 return $this->icon;
Chris@0 368 }
Chris@0 369
Chris@0 370 /**
Chris@0 371 * Sets the icon path for this layout definition.
Chris@0 372 *
Chris@0 373 * @param string|null $icon
Chris@0 374 * The icon path.
Chris@0 375 *
Chris@0 376 * @return $this
Chris@0 377 */
Chris@0 378 public function setIconPath($icon) {
Chris@0 379 $this->icon = $icon;
Chris@0 380 return $this;
Chris@0 381 }
Chris@0 382
Chris@0 383 /**
Chris@14 384 * Gets the icon map for this layout definition.
Chris@14 385 *
Chris@14 386 * This should not be used if an icon path is specified. See ::getIcon().
Chris@14 387 *
Chris@14 388 * @return string[][]|null
Chris@14 389 * The icon map, if it exists.
Chris@14 390 */
Chris@14 391 public function getIconMap() {
Chris@14 392 return $this->icon_map;
Chris@14 393 }
Chris@14 394
Chris@14 395 /**
Chris@14 396 * Sets the icon map for this layout definition.
Chris@14 397 *
Chris@14 398 * @param string[][]|null $icon_map
Chris@14 399 * The icon map.
Chris@14 400 *
Chris@14 401 * @return $this
Chris@14 402 */
Chris@14 403 public function setIconMap($icon_map) {
Chris@14 404 $this->icon_map = $icon_map;
Chris@14 405 return $this;
Chris@14 406 }
Chris@14 407
Chris@14 408 /**
Chris@14 409 * Builds a render array for an icon representing the layout.
Chris@14 410 *
Chris@14 411 * @param int $width
Chris@14 412 * (optional) The width of the icon. Defaults to 125.
Chris@14 413 * @param int $height
Chris@14 414 * (optional) The height of the icon. Defaults to 150.
Chris@14 415 * @param int $stroke_width
Chris@14 416 * (optional) If an icon map is used, the width of region borders.
Chris@14 417 * @param int $padding
Chris@14 418 * (optional) If an icon map is used, the padding between regions. Any
Chris@14 419 * value above 0 is valid.
Chris@14 420 *
Chris@14 421 * @return array
Chris@14 422 * A render array for the icon.
Chris@14 423 */
Chris@14 424 public function getIcon($width = 125, $height = 150, $stroke_width = NULL, $padding = NULL) {
Chris@14 425 $icon = [];
Chris@14 426 if ($icon_path = $this->getIconPath()) {
Chris@14 427 $icon = [
Chris@14 428 '#theme' => 'image',
Chris@14 429 '#uri' => $icon_path,
Chris@14 430 '#width' => $width,
Chris@14 431 '#height' => $height,
Chris@14 432 '#alt' => $this->getLabel(),
Chris@14 433 ];
Chris@14 434 }
Chris@14 435 elseif ($icon_map = $this->getIconMap()) {
Chris@14 436 $icon_builder = $this->getIconBuilder()
Chris@14 437 ->setId($this->id())
Chris@14 438 ->setLabel($this->getLabel())
Chris@14 439 ->setWidth($width)
Chris@14 440 ->setHeight($height);
Chris@14 441 if ($padding) {
Chris@14 442 $icon_builder->setPadding($padding);
Chris@14 443 }
Chris@14 444 if ($stroke_width) {
Chris@14 445 $icon_builder->setStrokeWidth($stroke_width);
Chris@14 446 }
Chris@14 447 $icon = $icon_builder->build($icon_map);
Chris@14 448 }
Chris@14 449 return $icon;
Chris@14 450 }
Chris@14 451
Chris@14 452 /**
Chris@14 453 * Wraps the icon builder.
Chris@14 454 *
Chris@14 455 * @return \Drupal\Core\Layout\Icon\IconBuilderInterface
Chris@14 456 * The icon builder.
Chris@14 457 */
Chris@14 458 protected function getIconBuilder() {
Chris@14 459 return \Drupal::service('layout.icon_builder');
Chris@14 460 }
Chris@14 461
Chris@14 462 /**
Chris@0 463 * Gets the regions for this layout definition.
Chris@0 464 *
Chris@0 465 * @return array[]
Chris@0 466 * The layout regions. The keys of the array are the machine names of the
Chris@0 467 * regions, and the values are an associative array with the following keys:
Chris@0 468 * - label: (string) The human-readable name of the region.
Chris@0 469 * Any remaining keys may have special meaning for the given layout plugin,
Chris@0 470 * but are undefined here.
Chris@0 471 */
Chris@0 472 public function getRegions() {
Chris@0 473 return $this->regions;
Chris@0 474 }
Chris@0 475
Chris@0 476 /**
Chris@0 477 * Sets the regions for this layout definition.
Chris@0 478 *
Chris@0 479 * @param array[] $regions
Chris@0 480 * An array of regions, see ::getRegions() for the format.
Chris@0 481 *
Chris@0 482 * @return $this
Chris@0 483 */
Chris@0 484 public function setRegions(array $regions) {
Chris@0 485 $this->regions = $regions;
Chris@0 486 return $this;
Chris@0 487 }
Chris@0 488
Chris@0 489 /**
Chris@0 490 * Gets the machine-readable region names.
Chris@0 491 *
Chris@0 492 * @return string[]
Chris@0 493 * An array of machine-readable region names.
Chris@0 494 */
Chris@0 495 public function getRegionNames() {
Chris@0 496 return array_keys($this->getRegions());
Chris@0 497 }
Chris@0 498
Chris@0 499 /**
Chris@0 500 * Gets the human-readable region labels.
Chris@0 501 *
Chris@0 502 * @return string[]
Chris@0 503 * An array of human-readable region labels.
Chris@0 504 */
Chris@0 505 public function getRegionLabels() {
Chris@0 506 $regions = $this->getRegions();
Chris@0 507 return array_combine(array_keys($regions), array_column($regions, 'label'));
Chris@0 508 }
Chris@0 509
Chris@0 510 /**
Chris@0 511 * Gets the default region.
Chris@0 512 *
Chris@0 513 * @return string
Chris@0 514 * The machine-readable name of the default region.
Chris@0 515 */
Chris@0 516 public function getDefaultRegion() {
Chris@0 517 return $this->default_region;
Chris@0 518 }
Chris@0 519
Chris@0 520 /**
Chris@0 521 * Sets the default region.
Chris@0 522 *
Chris@0 523 * @param string $default_region
Chris@0 524 * The machine-readable name of the default region.
Chris@0 525 *
Chris@0 526 * @return $this
Chris@0 527 */
Chris@0 528 public function setDefaultRegion($default_region) {
Chris@0 529 $this->default_region = $default_region;
Chris@0 530 return $this;
Chris@0 531 }
Chris@0 532
Chris@0 533 /**
Chris@0 534 * {@inheritdoc}
Chris@0 535 */
Chris@0 536 public function getDeriver() {
Chris@0 537 return $this->deriver;
Chris@0 538 }
Chris@0 539
Chris@0 540 /**
Chris@0 541 * {@inheritdoc}
Chris@0 542 */
Chris@0 543 public function setDeriver($deriver) {
Chris@0 544 $this->deriver = $deriver;
Chris@0 545 return $this;
Chris@0 546 }
Chris@0 547
Chris@0 548 }