Score:0

Help with Views Field Plugin

cn flag

I am trying to make a views field plugin that provides some additional functionality to a field for me. I am having issues with it.

I have an entity type called an lms_request. It has numerous bundles inside of it.

When using the custom 'views field plugin' on a view that specifically filters a single bundle, it works fine.

When I attempt to use the same field on a view that has multiple bundles filtered, or no bundles at all filtered (to show all), it throws an error preventing me from rendering the view. The error it spits out is this:

Notice: Undefined index: alter in Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender()

as well as Error: Unsupported operand types in Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender()

If I go into the fieldplugin definition and set override defineOptions

  public function defineOptions() {
    return parent::defineOptions();
  }

I receive the following in the dblog (3 times) Notice: Trying to access array offset on value of type null in Drupal\views\Plugin\views\field\FieldPluginBase->defineOptions()

I am confused why when filtering on a single bundle it works as expected, but as soon as I remove the filter or set the filter to multiple bundles, it breaks the field.

My hook_views_data() looks like this:

function lms_request_views_data() {
  $data['lms_request']['request_system_quick_edit'] = [
    'title' => t('Quick Edit Request'),
    'help' => t('Quick edit request form'),
    'field' => [
      'id' => 'request_system_quick_edit',
    ],
  ];

  return $data;
}

Where as 'request_system_quick_edit' is the name of the custom field plugin I made.

The field plugin code looks like this:

<?php

namespace Drupal\request_system\Plugin\views\field;

use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Provides Quick Edit field handler.
 *
 * @ViewsField("request_system_quick_edit")
 *
 * @DCG
 * The plugin needs to be assigned to a specific table column through
 * hook_views_data() or hook_views_data_alter().
 * For non-existent columns (i.e. computed fields) you need to override
 * self::query() method.
 */
class QuickEdit extends FieldPluginBase {

  protected $storage;
  protected $taxonomy;
  protected $library_item;

  /**
   * {@inheritdoc}
   */
  public function __construct() {
    $this->storage = \Drupal::entityTypeManager()->getStorage('lms_request');
    $this->taxonomy = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
    $this->library_item = \Drupal::entityTypeManager()->getStorage('item_type');
  }

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

  }

  // /**
  //  * {@inheritdoc}
  //  */
  protected function defineOptions() {
    parent::defineOptions();
  }

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

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $build['output']['#markup'] = 'Test';

    return $build;
  }
}

In addition, when adding the field the label and group shows up correctly, but once the field is in the view, it shows up a : instead of Group:Field Name Any help would be greatly appreciated.

4uk4 avatar
cn flag
Does the View use aggregation? If you look at computed field you usually see two things, the empty function query() and `public function usesGroupBy() { return FALSE; }`
Score:0
rs flag

I think the issue might be that you need to add a type for your render array item:

public function render(ResultRow $values) {
    $build['output'] = [
      '#type' => 'container',
      '#markup' => 'Test',
    ];

    return $build;
}
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.