LayoutDefinition.php 12 KB

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