FlexibleLayoutDeriver.php

Public

Get raw version
php
  1. <?php
  2. /**
  3.  * @file
  4.  * Contains \Drupal\my_layouts\Plugin\Deriver\FlexibleLayoutDeriver
  5.  */
  6. namespace Drupal\my_layouts\Plugin\Deriver;
  7.  
  8. use Drupal\Component\Plugin\Derivative\DeriverBase;
  9. use Drupal\node\Entity\Node;
  10.  
  11. /**
  12.  * Makes a flexible layout for each layout config entity.
  13.  *
  14.  * See https://www.drupal.org/node/2578731#using-php-annotations.
  15.  * See https://www.sitepoint.com/tutorial-on-using-drupal-8-plugin-derivatives-effectively/.
  16.  */
  17. class FlexibleLayoutDeriver extends DeriverBase {
  18. /**
  19.   * {@inheritdoc}
  20.   */
  21. public function getDerivativeDefinitions($base_plugin_definitions) {
  22.  
  23. /**
  24.   * @var $query \Drupal\Core\Entity\Query\QueryInterface
  25.   */
  26. // Query for all the node ids of the flexible_landing type.
  27. $nids = \Drupal::entityQuery('node')
  28. ->condition('type', 'flexible_landing')
  29. ->execute();
  30. // Loop over node ids and create the derivative for this layout.
  31. foreach ($nids as $nid) {
  32.  
  33. // Start with the base information.
  34. $derivative = $base_plugin_definitions;
  35.  
  36. // Create the label based on the node id so it is unique.
  37. $derivative['label'] = t('Flexible Layout @nid', ['@nid' => $nid]);
  38.  
  39. // Put all flexible layouts in one category.
  40. $derivative['category'] = t('Flexible Layouts');
  41.  
  42. // Now create a flexible layout based on the field_layout_regions
  43. // defined in the node. The field_layout_regions is a multiple value
  44. // field with each value having a text value like 2,4,2,4 that specifies
  45. // the "columns" in the row regions.
  46. //
  47. // Load the field_layout_regions field value.
  48. $layout_regions = Node::load($nid)->field_layout_regions->getValue();
  49.  
  50. // Build the regions array. Each region needs a unique machine name and
  51. // a label. The machine names are named in a way that the theme code
  52. // can determine the "columns" to use in SUSY.
  53. $derivative['regions'] = [];
  54. $row_count = 1;
  55. foreach ($layout_regions as $layout_row_array) {
  56. // Grab the value from multi-value field.
  57. $layout_row = $layout_row_array['value'];
  58. // Explode this since the format is like 2,4,2,4.
  59. $layout_row = explode(',', $layout_row_array['value']);
  60. $region_count = 1;
  61. foreach ($layout_row as $layout_row_num_columns) {
  62. // This will be a number like 3 or 6 that specifies the number
  63. // of SUSY "columns".
  64. // Get rid of spaces and make it an integer.
  65. $layout_row_num_columns = (int) trim($layout_row_num_columns);
  66. // Create a unique region name and label based on row, position in
  67. // the row, and number of "columns". The "columns" will be pulled
  68. // out later in the twig (?) file to do the right SUSY thing.
  69. $layout_row_item_name = sprintf('row_%d_region_%d_col_%d', $row_count, $region_count, $layout_row_num_columns);
  70. $layout_row_item_label = t('Row @row_count / Region @region_count / Cols @num_columns',
  71. [
  72. '@row_count' => $row_count,
  73. '@region_count' => $region_count,
  74. '@num_columns' => $layout_row_num_columns,
  75. ]
  76. );
  77. $derivative['regions'][$layout_row_item_name] = [
  78. 'label' => [
  79. 'string' => $layout_row_item_label,
  80. ],
  81. ];
  82. $region_count++;
  83. }
  84. $row_count++;
  85. }
  86.  
  87. // Use the $nid to register a layout for each unique node. An alternative
  88. // would be to create an admin page to create various named layouts and
  89. // use the machine name from the admin page here instead. That method
  90. // would mean that if there were 2 landing pages that had the same
  91. // layout, then it could be reused. This method has a new layout
  92. // per flexible_landing node.
  93. // @fixme: Figure out which way to go with the configuration.
  94. $this->derivatives[$nid] = $derivative;
  95. }
  96. return $this->derivatives;
  97. }
  98.  
  99. }