TypeStyle.php in Type Style 8
File
src/Plugin/views/field/TypeStyle.php
View source
<?php
namespace Drupal\type_style\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
class TypeStyle extends FieldPluginBase {
public function usesGroupBy() {
return FALSE;
}
public function query() {
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['type_style_name'] = [
'default' => '',
];
return $options;
}
protected function getStyleName() {
if ($this->realField !== 'type_style') {
$style_name = str_replace('type_style_', '', $this->realField);
}
else {
$style_name = $this->options['type_style_name'];
}
return $style_name;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
if ($this->realField === 'type_style') {
$form['type_style_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Type Style name'),
'#description' => $this
->t('The Type Style name, i.e. "color" or "icon".'),
'#default_value' => $this->options['type_style_name'],
];
}
}
public function render(ResultRow $values) {
return type_style_get_style($this
->getEntity($values), $this
->getStyleName(), '');
}
public function postRender(ResultRow $row, $output) {
parent::postRender($row, $output);
$style_name = $this
->getStyleName();
$render = $this
->render($row);
$replacements = [
'data-type-style-color-' . $style_name => 'style="color: ' . $render . '"',
'data-type-style-background-color-' . $style_name => 'style="background-color: ' . $render . '"',
];
if ($style_name === 'color') {
$replacements['data-type-style-color'] = 'style="color: ' . $render . '"';
$replacements['data-type-style-background-color'] = 'style="background-color: ' . $render . '"';
}
return $replacements;
}
}
Classes
Name |
Description |
TypeStyle |
A handler to output and arbitrary type style. |