You are here

public static function ExternalHreflang::getHrefLangsArrayFromString in External Hreflang 8

Convert string data from metatag to href langs array.

Parameters

string $value: Value.

Return value

array Array of Href Langs.

Throws

\Exception

3 calls to ExternalHreflang::getHrefLangsArrayFromString()
ExternalHreflang::output in src/Plugin/metatag/Tag/ExternalHreflang.php
Generate the HTML tag output for a meta tag.
ExternalHreflang::validateTag in src/Plugin/metatag/Tag/ExternalHreflang.php
Validates the metatag data.
_external_hreflang_get_metatags in ./external_hreflang.module
Load the meta tags by processing the route parameters.

File

src/Plugin/metatag/Tag/ExternalHreflang.php, line 111

Class

ExternalHreflang
A new hreflang tag will be made available for each language.

Namespace

Drupal\external_hreflang\Plugin\metatag\Tag

Code

public static function getHrefLangsArrayFromString(string $value = '') {
  $hreflangs = [];

  // Do nothing if empty.
  if (empty($value)) {
    return $hreflangs;
  }

  // Ensure we always have PHP_EOL as line separator.
  $value = str_replace("\r\n", PHP_EOL, $value);

  // Explode lines to get one array item per link.
  $value = array_filter(explode(PHP_EOL, $value));
  if (!is_array($value) || count($value) == 0) {
    throw new \Exception('Invalid value');
  }
  foreach ($value as $hreflang) {
    $hreflang = array_filter(explode('|', $hreflang));
    if (count($hreflang) !== 2) {
      throw new \Exception('Invalid value');
    }
    $hreflangs[$hreflang[0]] = $hreflang[1];
  }
  return $hreflangs;
}