You are here

public function LinkedFieldManager::linkHtml in Linked Field 8

Link HTML code with set link attributes.

Parameters

string $html: The list of HTML.

array $attributes: An associative array of attributes.

Return value

string Linking the html code.

Overrides LinkedFieldManagerInterface::linkHtml

File

src/LinkedFieldManager.php, line 272

Class

LinkedFieldManager
Provides helper methods for client related functionalities.

Namespace

Drupal\linked_field

Code

public function linkHtml($html, array $attributes) {

  // Convert HTML code to a DOMDocument object.
  $html_dom = Html::load($html);
  $body = $html_dom
    ->getElementsByTagName('body');
  $node = $body
    ->item(0);

  // Recursively walk over the DOMDocument body and place the links.
  $this
    ->linkNode($node, $html_dom, $attributes);

  // Converting the DOMDocument object back to HTML code.
  return Html::serialize($html_dom);
}