Create a View Plugin Without using Database field | Drupal 8

Create a View Plugin Without using Database field

Submitted by editor on Fri, 03/03/2017 - 13:53
Question

How to create a View Plugin Without using Database field , to use with an existence entity (Node, Taxonomy, user ...) ?

Step 1. Create a custom module and add the HOOK hook_views_data_alter(&$data). Here module : mymodule

Step 2. Alter views data, Example

function mymodule_views_data_alter(&$data) {
  //Add to Node views
  $data['node']['random_value'] = array(
    'field' => array(
      'id' => 'random_value',
      'title' => t('Random value'),
      'help' => t('Example of random value plugin.'),
    ),
  );

  //Add to Taxonomy views
  $data['taxonomy_term_data']['random_value'] = array(
    'field' => array(
      'id' => 'random_value',
      'title' => t('Random value'),
      'help' => t('Example of random value plugin.'),
    ),
  );

  //Add to Users views
  $data['users']['random_value'] = array(
    'field' => array(
      'id' => 'random_value',
      'title' => t('Random value'),
      'help' => t('Example of random value plugin.'),
    ),
  );

}

 

Step 3. Create view plugin on your custom mudule. (Here the minimum necessary to create a plugin)

 

Examlple : mymodule/src/Plugin/views/field/RandomValue.php

Plugin with minimim settings.

<?php

namespace Drupal\mymodule\Plugin\views\field;

use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\Core\Form\FormStateInterface;

/**
 * Default implementation of the base field plugin,
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("random_value")
 */
class RandomValue extends FieldPluginBase {

  /**
   * This is the most important part of this plugin.
   * Empty query methode, Allow to create field without database table field.
   */
  public function query() {
    //Must leave blank
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    //Retun your custom vaule, Here, a random value.
    return "Random value is : " . rand(0, 1000);
  }

}

 

 

Complete plugin with few other settings

<?php

namespace Drupal\mymodule\Plugin\views\field;

use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Drupal\Core\Form\FormStateInterface;

/**
 * Default implementation of the base field plugin,
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("random_value")
 */
class RandomValue extends FieldPluginBase {

  /**
   * This is the most important part of this plugin.
   * Empty query methode, Allow to create field without database table field.
   */
  public function query() {
    //Must leave blank
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['add_text'] = array('default' => 0);
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {

    $form['add_text'] = array(
      '#title' => t('Add a text'),
      '#type' => 'checkbox',
      '#default_value' => $this->options['add_text'],
    );

    parent::buildOptionsForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitOptionsForm(&$form, FormStateInterface $form_state) {

    $this->options['add_text'] = $form_state->getValue('add_text');

    parent::buildOptionsForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function adminSummary() {
    return $this->options['add_text'] ? "With text" : "no text";
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {

    return ($this->options['add_text'] ? "Random value is : " : "") . rand(0, 1000);
  }

}

 

Finally

Now you can add this field to your Node, Taxonomy and Users Views.

Add new comment