You are here

views_rss_yandex.module in Views RSS: Yandex Elements 8

Same filename and directory in other branches
  1. 6 views_rss_yandex.module
  2. 7 views_rss_yandex.module

Provides Yandex namespace and <item> elements for Views RSS module.

File

views_rss_yandex.module
View source
<?php

/**
 * @file
 * Provides Yandex namespace and <item> elements for Views RSS module.
 */
use Drupal\Core\Url;
use Drupal\Component\Utility\UrlHelper;

/**
 * Implements hook_views_rss_namespaces().
 */
function views_rss_yandex_views_rss_namespaces() {
  $namespaces['yandex'] = array(
    'prefix' => 'xmlns',
    'uri' => 'http://news.yandex.ru',
  );
  if (!\Drupal::moduleHandler()
    ->moduleExists('views_rss_media')) {
    $namespaces['media'] = array(
      'prefix' => 'xmlns',
      'uri' => 'http://search.yahoo.com/mrss/',
    );
  }
  return $namespaces;
}

/**
 * Implements hook_views_rss_channel_elements().
 */
function views_rss_yandex_views_rss_channel_elements() {
  $elements['yandex:logo'] = array(
    'description' => t('Path to the image to be used as the artwork for your feed, for example <em>sites/default/files/AllAboutEverything.jpg</em>. Allowed image formats are GIF, JPEG or PNG.'),
    'preprocess functions' => array(
      'views_rss_yandex_preprocess_channel_yandex_logo',
    ),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  $elements['yandex:logo type=square'] = array(
    'description' => t('Path to the image to be used as the artwork for your feed, for example <em>sites/default/files/AllAboutEverything.jpg</em>. Allowed image formats are GIF, JPEG or PNG. Minimum image width and height is 180 pixels.'),
    'preprocess functions' => array(
      'views_rss_yandex_preprocess_channel_yandex_logo_square',
    ),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  return $elements;
}

/**
 * Preprocess function for channel <yandex:logo> element.
 */
function views_rss_yandex_preprocess_channel_yandex_logo(&$variables) {

  // No value = no preprocessing.
  if (empty($variables['elements'][0]['value'])) {
    return;
  }
  $path = $variables['elements'][0]['value'];
  $variables['elements'][0]['value'] = file_create_url($path);
}

/**
 * Preprocess function for channel <yandex:logo type="square"> element.
 */
function views_rss_yandex_preprocess_channel_yandex_logo_square(&$variables) {

  // No value = no preprocessing.
  if (empty($variables['elements'][0]['value'])) {
    return;
  }

  // Change the element key and add the type="square" attribute.
  $variables['elements'][0]['key'] = 'yandex:logo';
  $variables['elements'][0]['attributes'] = array(
    'type' => 'square',
  );
  $path = $variables['elements'][0]['value'];
  $variables['elements'][0]['value'] = file_create_url($path);
}

/**
 * Implements hook_views_rss_item_elements().
 */
function views_rss_yandex_views_rss_item_elements() {
  $elements['yandex:full-text'] = array(
    'description' => t('Full message text for search index.'),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  $elements['yandex:genre'] = array(
    'description' => t('Should be either lenta, message, article or interview.'),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  $elements['yandex:tags'] = array(
    'description' => t('Tags, separate multiple items with comma.'),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
    'preprocess functions' => array(
      'views_rss_core_preprocess_channel_category',
    ),
  );
  $elements['yandex:online'] = array(
    'description' => t('Link to online broadcast xml feed.'),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  $elements['yandex:enclosure'] = array(
    'description' => t('Handles several enclosure elements per item.'),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  return $elements;
}

/**
 * Implements hook_views_rss_options_form_validate().
 */
function views_rss_yandex_views_rss_options_form_validate($form, $form_state) {
  $form_state_values = $form_state
    ->getValues();

  // Validate channel <yandex:logo> element.
  if (!empty($form_state_values['style_options']['channel']['yandex']['views_rss_yandex']['logo'])) {

    // Do not validate absolute URLs, as this could mean external image.
    if (!UrlHelper::isValid($form_state_values['style_options']['channel']['yandex']['views_rss_yandex']['logo'], TRUE)) {

      // Check that image exists and is in acceptable format.
      $image = \Drupal::service('image.factory')
        ->get($form_state_values['style_options']['channel']['yandex']['views_rss_yandex']['logo']);
      if (!$image
        ->isValid()) {
        $form_state
          ->setErrorByName('style_options][channel][yandex][views_rss_yandex][logo', t('Unable to find %image or incorrect image format.', array(
          '%image' => $form_state_values['style_options']['channel']['yandex']['views_rss_yandex']['logo'],
        )));
      }
    }
  }

  // Validate channel <yandex:logo type="square"> element.
  if (!empty($form_state_values['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square'])) {

    // Do not validate absolute URLs, as this could mean external image.
    if (!UrlHelper::isValid($form_state_values['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square'], TRUE)) {

      // Check that image exists and is in acceptable format.
      $image = \Drupal::service('image.factory')
        ->get($form_state_values['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square']);
      if (!$image
        ->isValid()) {
        $form_state
          ->setErrorByName('style_options][channel][yandex][views_rss_yandex][logo type=square', t('Unable to find %image or incorrect image format.', array(
          '%image' => $form_state_values['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square'],
        )));
      }
      else {

        // Check image width and height.
        if ($image
          ->getWidth() < 180 || $image
          ->getHeight() < 180) {
          $form_state
            ->setErrorByName('style_options][channel][yandex][views_rss_yandex][logo type=square', t("Minimum allowed width/height of an image for feed channel's &lt;yandex:logo type=square&gt; element is 180 pixels. Specified %image is %width x %height pixels.", array(
            '%image' => $form_state_values['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square'],
            '%width' => $image
              ->getWidth(),
            '%height' => $image
              ->getHeight(),
          )));
        }
        elseif ($image
          ->getWidth() != $image
          ->getHeight()) {
          $form_state
            ->setErrorByName('style_options][channel][yandex][views_rss_yandex][logo type=square', t("Width and height of an image for feed channel's &lt;yandex:logo type=square&gt; element should be equal. Specified %image is %width x %height pixels.", array(
            '%image' => $form_state_values['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square'],
            '%width' => $image
              ->getWidth(),
            '%height' => $image
              ->getHeight(),
          )));
        }
      }
    }
  }
}

Functions

Namesort descending Description
views_rss_yandex_preprocess_channel_yandex_logo Preprocess function for channel <yandex:logo> element.
views_rss_yandex_preprocess_channel_yandex_logo_square Preprocess function for channel <yandex:logo type="square"> element.
views_rss_yandex_views_rss_channel_elements Implements hook_views_rss_channel_elements().
views_rss_yandex_views_rss_item_elements Implements hook_views_rss_item_elements().
views_rss_yandex_views_rss_namespaces Implements hook_views_rss_namespaces().
views_rss_yandex_views_rss_options_form_validate Implements hook_views_rss_options_form_validate().