9c0c3d707bdee5500fb521caf470dc63a26aa919
[yaffs-website] / web / core / modules / views / tests / modules / views_test_data / src / Plugin / views / style / StyleTest.php
1 <?php
2
3 namespace Drupal\views_test_data\Plugin\views\style;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\style\StylePluginBase;
7
8 /**
9  * Provides a general test style plugin.
10  *
11  * @ingroup views_style_plugins
12  *
13  * @ViewsStyle(
14  *   id = "test_style",
15  *   title = @Translation("Test style plugin"),
16  *   help = @Translation("Provides a generic style test plugin."),
17  *   theme = "views_view_style_test",
18  *   register_theme = FALSE,
19  *   display_types = {"normal", "test"}
20  * )
21  */
22 class StyleTest extends StylePluginBase {
23
24   /**
25    * A string which will be output when the view is rendered.
26    *
27    * @var string
28    */
29   public $output;
30
31   /**
32    * Can the style plugin use row plugins.
33    *
34    * @var bool
35    */
36   protected $usesRowPlugin = TRUE;
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function defineOptions() {
42     $options = parent::defineOptions();
43     $options['test_option'] = ['default' => ''];
44
45     return $options;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
52     parent::buildOptionsForm($form, $form_state);
53
54     $form['test_option'] = [
55       '#title' => $this->t('Test option'),
56       '#type' => 'textfield',
57       '#description' => $this->t('This is a textfield for test_option.'),
58       '#default_value' => $this->options['test_option'],
59     ];
60   }
61
62   /**
63    * Sets the usesRowPlugin property.
64    *
65    * @param bool $status
66    *   TRUE if this style plugin should use rows.
67    */
68   public function setUsesRowPlugin($status) {
69     $this->usesRowPlugin = $status;
70   }
71
72   /**
73    * Sets the output property.
74    *
75    * @param string $output
76    *   The string to output by this plugin.
77    */
78   public function setOutput($output) {
79     $this->output = $output;
80   }
81
82   /**
83    * Returns the output property.
84    *
85    * @return string
86    */
87   public function getOutput() {
88     return $this->output;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function render() {
95     $output = '';
96     if (!$this->usesRowPlugin()) {
97       $output = $this->getOutput();
98     }
99     else {
100       foreach ($this->view->result as $index => $row) {
101         $this->view->row_index = $index;
102         $output .= $this->view->rowPlugin->render($row) . "\n";
103       }
104     }
105
106     return $output;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function calculateDependencies() {
113     return [
114       'content' => ['StyleTest'],
115     ];
116   }
117
118 }