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

/**
 * @file
5
 * Definition of Drupal\views\Plugin\views\field\MachineName.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Plugin\views\field;
9

10
use Drupal\Component\Utility\String;
11
use Drupal\views\ResultRow;
dawehner's avatar
dawehner committed
12

merlinofchaos's avatar
merlinofchaos committed
13 14 15 16 17 18 19
/**
 * Field handler whichs allows to show machine name content as human name.
 * @ingroup views_field_handlers
 *
 * Definition items:
 * - options callback: The function to call in order to generate the value options. If omitted, the options 'Yes' and 'No' will be used.
 * - options arguments: An array of arguments to pass to the options callback.
20
 *
21
 * @ViewsField("machine_name")
dawehner's avatar
dawehner committed
22
 */
23
class MachineName extends FieldPluginBase {
24

merlinofchaos's avatar
merlinofchaos committed
25 26 27
  /**
   * @var array Stores the available options.
   */
28
  protected $valueOptions;
merlinofchaos's avatar
merlinofchaos committed
29

30
  public function getValueOptions() {
31
    if (isset($this->valueOptions)) {
merlinofchaos's avatar
merlinofchaos committed
32 33 34 35 36
      return;
    }

    if (isset($this->definition['options callback']) && is_callable($this->definition['options callback'])) {
      if (isset($this->definition['options arguments']) && is_array($this->definition['options arguments'])) {
37
        $this->valueOptions = call_user_func_array($this->definition['options callback'], $this->definition['options arguments']);
merlinofchaos's avatar
merlinofchaos committed
38 39
      }
      else {
40
        $this->valueOptions = call_user_func($this->definition['options callback']);
merlinofchaos's avatar
merlinofchaos committed
41 42 43
      }
    }
    else {
44
      $this->valueOptions = array();
merlinofchaos's avatar
merlinofchaos committed
45 46 47
    }
  }

48 49
  protected function defineOptions() {
    $options = parent::defineOptions();
merlinofchaos's avatar
merlinofchaos committed
50 51 52 53 54
    $options['machine_name'] = array('default' => FALSE, 'bool' => TRUE);

    return $options;
  }

55 56
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
57 58 59 60 61 62 63 64 65

    $form['machine_name'] = array(
      '#title' => t('Output machine name'),
      '#description' => t('Display field as machine name.'),
      '#type' => 'checkbox',
      '#default_value' => !empty($this->options['machine_name']),
    );
  }

66
  public function preRender(&$values) {
67
    $this->getValueOptions();
merlinofchaos's avatar
merlinofchaos committed
68 69
  }

70 71 72 73
  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
merlinofchaos's avatar
merlinofchaos committed
74
    $value = $values->{$this->field_alias};
75
    if (!empty($this->options['machine_name']) || !isset($this->valueOptions[$value])) {
76
      $result = String::checkPlain($value);
merlinofchaos's avatar
merlinofchaos committed
77 78
    }
    else {
79
      $result = $this->valueOptions[$value];
merlinofchaos's avatar
merlinofchaos committed
80 81 82 83
    }

    return $result;
  }
84

merlinofchaos's avatar
merlinofchaos committed
85
}