Category.php 1.96 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Views\aggregator\Plugin\views\field\Category.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Views\aggregator\Plugin\views\field;
dawehner's avatar
dawehner committed
9

10
use Drupal\views\Plugin\views\field\FieldPluginBase;
dawehner's avatar
dawehner committed
11
use Drupal\Core\Annotation\Plugin;
12

merlinofchaos's avatar
merlinofchaos committed
13 14 15 16 17
/**
 * Field handler to provide simple renderer that allows linking to aggregator
 * category.
 *
 * @ingroup views_field_handlers
18
 *
dawehner's avatar
dawehner committed
19
 * @Plugin(
20 21
 *   id = "aggregator_category",
 *   module = "aggregator"
dawehner's avatar
dawehner committed
22 23 24
 * )
 */
class Category extends FieldPluginBase {
25

merlinofchaos's avatar
merlinofchaos committed
26
  /**
27
   * Constructs a Category object.
merlinofchaos's avatar
merlinofchaos committed
28
   */
29 30 31
  public function __construct(array $configuration, $plugin_id, DiscoveryInterface $discovery) {
    parent::__construct($configuration, $plugin_id, $discovery);

merlinofchaos's avatar
merlinofchaos committed
32 33 34
    $this->additional_fields['cid'] = 'cid';
  }

35 36
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
37 38 39 40 41 42 43
    $options['link_to_category'] = array('default' => FALSE, 'bool' => TRUE);
    return $options;
  }

  /**
   * Provide link to category option
   */
44
  public function buildOptionsForm(&$form, &$form_state) {
merlinofchaos's avatar
merlinofchaos committed
45 46 47 48 49 50
    $form['link_to_category'] = array(
      '#title' => t('Link this field to its aggregator category page'),
      '#description' => t('This will override any other link you have set.'),
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['link_to_category']),
    );
51
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  }

  /**
   * Render whatever the data is as a link to the category.
   *
   * Data should be made XSS safe prior to calling this function.
   */
  function render_link($data, $values) {
    $cid = $this->get_value($values, 'cid');
    if (!empty($this->options['link_to_category']) && !empty($cid) && $data !== NULL && $data !== '') {
      $this->options['alter']['make_link'] = TRUE;
      $this->options['alter']['path'] = "aggregator/category/$cid";
    }
    return $data;
  }

  function render($values) {
    $value = $this->get_value($values);
70
    return $this->render_link($this->sanitizeValue($value), $values);
merlinofchaos's avatar
merlinofchaos committed
71
  }
72

merlinofchaos's avatar
merlinofchaos committed
73
}