3 namespace Drupal\views\Plugin\views\field;
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Render\ViewsRenderPipelineMarkup;
8 use Drupal\views\ResultRow;
11 * A handler to provide a field that is completely custom by the administrator.
13 * @ingroup views_field_handlers
15 * @ViewsField("custom")
17 class Custom extends FieldPluginBase {
22 public function usesGroupBy() {
29 public function query() {
30 // do nothing -- to override the parent query.
36 protected function defineOptions() {
37 $options = parent::defineOptions();
39 // Override the alter text option to always alter the text.
40 $options['alter']['contains']['alter_text'] = ['default' => TRUE];
41 $options['hide_alter_empty'] = ['default' => FALSE];
48 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
49 parent::buildOptionsForm($form, $form_state);
51 // Remove the checkbox
52 unset($form['alter']['alter_text']);
53 unset($form['alter']['text']['#states']);
54 unset($form['alter']['help']['#states']);
55 $form['#pre_render'][] = [$this, 'preRenderCustomForm'];
61 public function render(ResultRow $values) {
62 // Return the text, so the code never thinks the value is empty.
63 return ViewsRenderPipelineMarkup::create(Xss::filterAdmin($this->options['alter']['text']));
67 * Prerender function to move the textarea to the top of a form.
70 * The form build array.
73 * The modified form build array.
75 public function preRenderCustomForm($form) {
76 $form['text'] = $form['alter']['text'];
77 $form['help'] = $form['alter']['help'];
78 unset($form['alter']['text']);
79 unset($form['alter']['help']);