Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Layout / LayoutDefinition.php
index c804776840d18f5350f88d4a5a6e58bb0b082223..c87b618d113f50b8d96cac0a6306f58cd9560bc1 100644 (file)
@@ -85,6 +85,15 @@ class LayoutDefinition extends PluginDefinition implements PluginDefinitionInter
    */
   protected $icon;
 
+  /**
+   * An array defining the regions of a layout.
+   *
+   * @var string[][]|null
+   *
+   * @see \Drupal\Core\Layout\Icon\IconBuilderInterface::build()
+   */
+  protected $icon_map;
+
   /**
    * An associative array of regions in this layout.
    *
@@ -371,6 +380,85 @@ class LayoutDefinition extends PluginDefinition implements PluginDefinitionInter
     return $this;
   }
 
+  /**
+   * Gets the icon map for this layout definition.
+   *
+   * This should not be used if an icon path is specified. See ::getIcon().
+   *
+   * @return string[][]|null
+   *   The icon map, if it exists.
+   */
+  public function getIconMap() {
+    return $this->icon_map;
+  }
+
+  /**
+   * Sets the icon map for this layout definition.
+   *
+   * @param string[][]|null $icon_map
+   *   The icon map.
+   *
+   * @return $this
+   */
+  public function setIconMap($icon_map) {
+    $this->icon_map = $icon_map;
+    return $this;
+  }
+
+  /**
+   * Builds a render array for an icon representing the layout.
+   *
+   * @param int $width
+   *   (optional) The width of the icon. Defaults to 125.
+   * @param int $height
+   *   (optional) The height of the icon. Defaults to 150.
+   * @param int $stroke_width
+   *   (optional) If an icon map is used, the width of region borders.
+   * @param int $padding
+   *   (optional) If an icon map is used, the padding between regions. Any
+   *   value above 0 is valid.
+   *
+   * @return array
+   *   A render array for the icon.
+   */
+  public function getIcon($width = 125, $height = 150, $stroke_width = NULL, $padding = NULL) {
+    $icon = [];
+    if ($icon_path = $this->getIconPath()) {
+      $icon = [
+        '#theme' => 'image',
+        '#uri' => $icon_path,
+        '#width' => $width,
+        '#height' => $height,
+        '#alt' => $this->getLabel(),
+      ];
+    }
+    elseif ($icon_map = $this->getIconMap()) {
+      $icon_builder = $this->getIconBuilder()
+        ->setId($this->id())
+        ->setLabel($this->getLabel())
+        ->setWidth($width)
+        ->setHeight($height);
+      if ($padding) {
+        $icon_builder->setPadding($padding);
+      }
+      if ($stroke_width) {
+        $icon_builder->setStrokeWidth($stroke_width);
+      }
+      $icon = $icon_builder->build($icon_map);
+    }
+    return $icon;
+  }
+
+  /**
+   * Wraps the icon builder.
+   *
+   * @return \Drupal\Core\Layout\Icon\IconBuilderInterface
+   *   The icon builder.
+   */
+  protected function getIconBuilder() {
+    return \Drupal::service('layout.icon_builder');
+  }
+
   /**
    * Gets the regions for this layout definition.
    *