1

i want a vitual field for print value of sum in another entity :

My 2 custom entity content :

commandes_entity

commandesLignes_entity reference to commandes_entity

In the commandes_entity i have a view table who liste all my lines. i want to add a custom column who sum all the children in commandesLignes_entity.

For that i make a field in .module :

function hook_views_data()
{

  $data['commandes_entity'] = array(

      'totalCommandeTTC' => array(
          'title' => t('Total Commande TTC'),
          'help' => t("Le total des commandes"),
          'field' => array(
              'id' => 'machine_name',
              'options callback' => 'Drupal\boutique\Controller\TestController::content',

          ),

          'sort' => array(
              'id' => 'standard',
          ),

      ),

  );

return $data;
}

This field is in my select field view, when i add it all is ok except one thing :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'commandes_entity.totalCommandeTTC' in 'field list': SELECT commandes_entity.totalCommandeTTC AS commandes_entity_totalcommandettc, commandes_entity.id AS id FROM {commandes_entity} commandes_entity WHERE (( (commandes_entity.statut LIKE :db_condition_placeholder_0 ESCAPE '\') )) ORDER BY commandes_entity.created DESC LIMIT 21 OFFSET 0; Array ( [:db_condition_placeholder_0] => payer )

This field

totalCommandeTTC

doesn't exist in my table, i want it virtual.

For now my callback just return 'test'.

Any idea for make this field really virtual ?

I try to move it to views group like this :

function boutique_views_data()
{

  $data['views'] = array(

      'totalCommandeTTC' => array(
          'title' => t('Total Commande TTC'),
          'help' => t("Le total des commandes"),
          'field' => array(
              'id' => 'machine_name',
              'options callback' => 'Drupal\boutique\Controller\TestController::content',

          ),

          'sort' => array(
              'id' => 'standard',
          ),

      ),

  );

return $data;
}

The field is in the Global group but SQL still try to get it into the commandes_entity table.

2
  • 1
    In D7 you needed to provide a custom handler for the field, and override the query method. It's probably very similar in D8 but I'm not at the computer to check at the moment
    – Clive
    Jun 30, 2016 at 15:37
  • ok thx Clive i ll check how its done in d7 and try to implement that into d8
    – Kevin
    Jun 30, 2016 at 20:28

1 Answer 1

3

Thank to Clive for guide me :

This is how i make a virtual field :

First you need to create the handler for the field :

<?php

namespace Drupal\boutique\Plugin\views\field;

use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;

/**
 * Field handler to present the path to the node.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("totalCommandeTTC")
 */
class TotalTTC extends FieldPluginBase {

    /**
     * {@inheritdoc}
     */
    public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
        parent::init($view, $display, $options);

    }

    /**
     * {@inheritdoc}
     */
    public function query() {
        }

    /**
     * {@inheritdoc}
     */
    public function render(ResultRow $values) {
       return 'test';
    }

}

I remove the query for this handler.

Then in my module file, i use the hook_views_data :

function boutique_views_data()
{

  $data['commandes_entity'] = array(

      'totalCommandeTTC' => array(
          'title' => t('Total Commande TTC'),
          'help' => t("Le total TTC des commandes"),
          'field' => array(
              'id' => 'totalCommandeTTC',
          ),


      ),

  );

return $data;
}

And the result in views after add the field with views ui :

enter image description here

The field is Total commande TTC.

1
  • 1
    Lovely answer, it is helpful.
    – Jimmy Ko
    Jul 1, 2016 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.