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;
}
}