You are here

trait PodcastViewsMappingsTrait in Podcast (using Views) 8

Trait that contains common mapping tasks for channel and item level mappings.

Hierarchy

2 files declare their use of PodcastViewsMappingsTrait
Rss.php in src/Plugin/views/style/Rss.php
RssFields.php in src/Plugin/views/row/RssFields.php

File

src/PodcastViewsMappingsTrait.php, line 11

Namespace

Drupal\podcast
View source
trait PodcastViewsMappingsTrait {

  /**
   * Builds the channel element from the options.
   *
   * @param string $key
   *   The name of the key.
   * @param int $row_index
   *   The index of the row to get the fields from.
   * @param string[] $options_parents
   *   The parents to extract the field name from the config options.
   *
   * @return array
   *   Key value array for the property.
   */
  protected function buildElementFromOptions($key, $row_index = 0, array $options_parents = NULL) {
    if (empty($options_parents)) {
      $options_parents = [
        sprintf('%s_field', $key),
      ];
    }
    $field_name = NestedArray::getValue($this->options, $options_parents);
    if (empty($field_name)) {
      return [];
    }
    $markup = $this
      ->getField($row_index, $field_name);
    return $markup ? [
      'key' => $key,
      'value' => $markup,
    ] : [];
  }

  /**
   * Same as buildElementFromOptions but generates full URLs.
   *
   * @param string $key
   *   The name of the key.
   * @param int $row_index
   *   The index of the row to get the fields from.
   * @param string[] $options_parents
   *   The parents to extract the field name from the config options.
   *
   * @return array
   *   Key value array for the property.
   */
  protected function buildElementForLink($key, $row_index = 0, array $options_parents = NULL) {
    $build = $this
      ->buildElementFromOptions($key, $row_index, $options_parents);
    if (empty($build['value'])) {
      return [];
    }

    // Make sure to cast to a string for the Url conversion.
    $value = (string) $build['value'];

    // Do not convert to an absolute URL if it already is one.
    if (preg_match('/https?:/', $value)) {
      return [
        'key' => $key,
        'value' => $value,
      ];
    }
    $link_input = '/' . ltrim($value, '/');
    try {
      return [
        'key' => $key,
        'value' => Url::fromUserInput($link_input)
          ->setAbsolute()
          ->toString(),
      ];
    } catch (\InvalidArgumentException $exception) {
      watchdog_exception('podcast', $exception);
    }
    return [];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PodcastViewsMappingsTrait::buildElementForLink protected function Same as buildElementFromOptions but generates full URLs.
PodcastViewsMappingsTrait::buildElementFromOptions protected function Builds the channel element from the options.