diff --git a/core/lib/Drupal/Core/Link.php b/core/lib/Drupal/Core/Link.php index a681bbccf9..d3f115e07f 100644 --- a/core/lib/Drupal/Core/Link.php +++ b/core/lib/Drupal/Core/Link.php @@ -37,11 +37,11 @@ class Link implements RenderableInterface { protected $attributes = []; /** - * An indicator of whether this is a dialog link. + * The link attributes that are needed for dialog use. * - * @var bool + * @var array */ - protected $isDialog = FALSE; + protected $dialogAttributes; /** * Returns the link attributes. @@ -50,7 +50,16 @@ class Link implements RenderableInterface { * The link attributes. */ public function getAttributes() { - return $this->attributes; + // @todo Should we not merge dialogAttributes and attributes here? + // Merging gives that attributes that will actually be used. + $attributes = $this->attributes; + if ($this->dialogAttributes) { + if (!isset($attributes['class']) || !in_array('use-ajax', $attributes['class'])) { + $attributes['class'][] = 'use-ajax'; + } + $attributes = array_merge($attributes, $this->dialogAttributes); + } + return $attributes; } /** @@ -167,6 +176,7 @@ public function setUrl(Url $url) { * @see \Drupal\Core\Link::toRenderable() */ public function toString() { + // @todo Ensure this method takes into account attributes. return $this->getLinkGenerator()->generateFromLink($this); } @@ -178,11 +188,9 @@ public function toRenderable() { '#type' => 'link', '#url' => $this->url, '#title' => $this->text, + '#attributes' => $this->getAttributes(), ]; - if ($this->attributes) { - $renderable['#attributes'] = $this->attributes; - } - if ($this->isDialog) { + if ($this->dialogAttributes) { $renderable['#attached'] = [ 'library' => [ 'core/drupal.dialog.ajax', @@ -199,11 +207,16 @@ public function toRenderable() { * The dialog type 'modal' or 'dialog', defaults to 'modal'. * @param string $renderer * The dialog renderer. Core provides the 'off_canvas' renderer which uses - * the off-canvas dialog. + * the off-canvas dialog. Other modules can provide dialog renderers by + * defining a service that is tagged with the name + * 'render.main_content_renderer' and tagged with a format in the pattern of + * 'drupal_[dialogType].[dialogRenderer]'. * @param array $options * The dialog options. + * + * @return $this */ - public function openInDailog($type = 'modal', $renderer = NULL, $options = []) { + public function openInDialog($type = 'modal', $renderer = NULL, array $options = []) { if (!in_array($type, ['dialog', 'modal'])) { throw new \UnexpectedValueException("The dialog type must be either 'dialog' or 'modal'"); } @@ -213,18 +226,13 @@ public function openInDailog($type = 'modal', $renderer = NULL, $options = []) { if (!isset($main_content_renders[$renderer_key])) { throw new \UnexpectedValueException("The renderer '$renderer_key' is not available."); } - if (!isset($this->attributes['class']) || !in_array('use-ajax', $this->attributes['class'])) { - $this->attributes['class'][] = 'use-ajax'; - } - $this->attributes['data-dialog-type'] = $type; + $this->dialogAttributes['data-dialog-type'] = $type; if ($renderer) { - $this->attributes['data-dialog-renderer'] = $renderer; + $this->dialogAttributes['data-dialog-renderer'] = $renderer; } if ($options) { - $this->attributes['data-dialog-options'] = json_encode($options); + $this->dialogAttributes['data-dialog-options'] = json_encode($options); } - $this->isDialog = TRUE; - return $this; }