Score:0

Views query plugin Undefined Index error

ua flag
mfv

I have been trying to build a custom views query plugin but Im getting an undefined index error for each field that i'm mapping. The filters im using for the endpoint do work when i check them. This plugin pulls data from an another drupal site through a json endpoint. I am mapping the fields to views with hook_views_data():

<?php
/**
 * Implements hook_views_data().
 */
function letter_query_views_data(){
  $data =[];
  //base data
$data['letter_query']['table']['group'] = t('Letter Query');
  $data['letter_query']['table']['base'] = [
  'title' => t('Letter Query'),
    'query_id' => 'views_letter_query',
    'help' => t('Querying API endpoint for letters'),
  ];

//fields
  $data['letter_query']['title']=[
    'title' => t('Letter Title'),
    'help' => t('Title of the letter'),
    'field' => [
      'id' => 'standard',
    ],
  ];

  $data['letter_query']['field_note']=[
    'title' => t('Note'),
    'help' => t('Letter Note'),
    'field' => [
      'id' => 'standard',
    ],
  ];

return $data;
}

Here is my views query:

<?php

namespace Drupal\letter_query\Plugin\views\query;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\Annotation\ViewsQuery;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Views query plugin which wraps calls to the letter query API in order to
 * expose the results to views.
 *
 * @ViewsQuery(
 *   id = "views_letter_query",
 *   title = @Translation("Letter Query"),
 *   help = @Translation("Query against the API.")
 * )
 */

class ViewsLetterQuery extends QueryPluginBase implements ContainerFactoryPluginInterface {

  /**
   * @var \Drupal\letter_query\Plugin\views\letterQueryClient
   */
  protected $letterQueryClient;


  /**
   * ViewsLetterQuery constructor.
   *
   * @param array $configuration
   * @param $plugin_id
   * @param $plugin_definition
   * @param $letterQueryClient \Drupal\letter_query\Plugin\views\letterQueryClient
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, $letterQueryClient) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->letterQueryClient = $letterQueryClient;

  }

  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('letter_query_client')

    );
  }

  public function execute(ViewExecutable $view) {
    $index = 0;
    if ($data = $this->letterQueryClient->getLetters())
    {
      foreach ($data as $letter_item) {
        $row['title'] = $letter_item['title'];
        $row['field_note'] = $letter_item['field_note'];
        $row['index'] = $index++;
        $view->result[] = new ResultRow($row);

      }

    }


  }

  public function ensureTable($table, $relationship = NULL) {
    return '';
  }

  public function addField($table, $field, $alias = '', $params = []) {
    return $field;
  }
}
in flag
What type of error(s) are you receiving? I suspect that you might also need to define a @ViewsFilter plugin for the filter portion: https://zanzarra.com/blog/custom-views-filter-plugin-drupal-8-bounding-box-geofield
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.