View source
<?php
namespace Drupal\views_xml_backend\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Markup extends Standard {
protected $currentUser;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $current_user, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $current_user;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_user'), $container
->get('renderer'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['format']['default'] = '';
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$options = [];
foreach (filter_formats($this->currentUser) as $id => $format) {
$options[$id] = $format
->get('name');
}
$form['format'] = [
'#title' => $this
->t('Format'),
'#description' => $this
->t('The filter format'),
'#type' => 'select',
'#default_value' => $this->options['format'],
'#required' => TRUE,
'#options' => $options,
];
}
public function render_item($count, $item) {
$text = str_replace('<!--break-->', '', $item['value']);
$build = [
'#type' => 'processed_text',
'#text' => $text,
'#format' => $this->options['format'],
'#filter_types_to_skip' => [],
'#langcode' => '',
];
return $this->renderer
->renderPlain($build);
}
}