Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create a View Plugin Without using Database field

By editor , 3 March, 2017
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.

Tags

  • Drupal 8
  • Code
  • Module
  • Views
Views with custom Tables - hook_views_data

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer