You are here

public function PinterestEmbedFormatter::viewElements in Media entity Pinterest 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/PinterestEmbedFormatter.php \Drupal\media_entity_pinterest\Plugin\Field\FieldFormatter\PinterestEmbedFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/PinterestEmbedFormatter.php, line 26

Class

PinterestEmbedFormatter
Plugin implementation of the 'pinterest_embed' formatter.

Namespace

Drupal\media_entity_pinterest\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = [];
  foreach ($items as $delta => $item) {
    $matches = [];
    foreach (Pinterest::$validationRegexp as $pattern => $key) {

      // URLs will sometimes have urlencoding, so we decode for matching.
      if (preg_match($pattern, urldecode($this
        ->getEmbedCode($item)), $item_matches)) {
        $matches[] = $item_matches;
      }
    }
    if (!empty($matches)) {
      $matches = reset($matches);
    }

    // PIN_URL_RE matched.
    if (!empty($matches['id']) && isset($matches[2]) && isset($matches[3]) && isset($matches[4])) {
      $element[$delta] = [
        '#theme' => 'media_entity_pinterest_pin',
        '#path' => 'https://' . $matches[2] . 'pinterest.' . $matches[3] . $matches[4] . '/pin/' . $matches['id'],
        '#attributes' => [
          'class' => [],
          'data-conversation' => 'none',
          'lang' => $langcode,
        ],
      ];
    }

    // BOARD_SECTION_URL_RE matched.
    if (!empty($matches['username']) && !empty($matches['slug']) && !empty($matches['section']) && isset($matches[2]) && isset($matches[3]) && isset($matches[4])) {
      $element[$delta] = [
        '#theme' => 'media_entity_pinterest_board_section',
        '#path' => 'https://' . $matches[2] . 'pinterest.' . $matches[3] . $matches[4] . '/' . $matches['username'] . '/' . $matches['slug'] . '/' . $matches['section'],
        '#attributes' => [
          'class' => [],
          'data-conversation' => 'none',
          'lang' => $langcode,
        ],
      ];
    }

    // BOARD_URL_RE matched.
    if (!empty($matches['username']) && !empty($matches['slug']) && empty($matches['section']) && isset($matches[2]) && isset($matches[3]) && isset($matches[4])) {
      $element[$delta] = [
        '#theme' => 'media_entity_pinterest_board',
        '#path' => 'https://' . $matches[2] . 'pinterest.' . $matches[3] . $matches[4] . '/' . $matches['username'] . '/' . $matches['slug'],
        '#attributes' => [
          'class' => [],
          'data-conversation' => 'none',
          'lang' => $langcode,
        ],
      ];
    }

    // USER_URL_RE matched.
    if (!empty($matches['username']) && empty($matches['slug']) && empty($matches['section']) && isset($matches[2]) && isset($matches[3]) && isset($matches[4])) {
      $element[$delta] = [
        '#theme' => 'media_entity_pinterest_profile',
        '#path' => 'https://' . $matches[2] . 'pinterest.' . $matches[3] . $matches[4] . '/' . $matches['username'],
        '#attributes' => [
          'class' => [],
          'data-conversation' => 'none',
          'lang' => $langcode,
        ],
      ];
    }
  }
  if (!empty($element)) {
    $element['#attached'] = [
      'library' => [
        'media_entity_pinterest/integration',
      ],
    ];
  }
  return $element;
}