Hi,

I am facing some major issues while using solr indexed field in views.

Issue :
PHP Fatal error: Call to a member function getType() on a non-object in /var/www/html/core/modules/views/src/Plugin/views/field/Field.php on line 348getType()

Also I am getting notice for using fields like price from ubercart.

Notice: Undefined index: price in Drupal\views\Plugin\views\field\Field->getFieldStorageDefinition() (line 333 of /var/www/html/core/modules/views/src/Plugin/views/field/Field.php).

Kindly help.

Regards,
Jaswinder

Comments

chera.jaswinder created an issue. See original summary.

chera.jaswinder’s picture

Issue summary: View changes
Issue tags: -Indexed views field +Search API, +search api solr, +Search API index
lendude’s picture

Assigned: chera.jaswinder » Unassigned
Status: Active » Postponed (maintainer needs more info)

This sounds like an issue with Search API Views, which isn't part of the Views module. Moving it to the Search API queue.

lendude’s picture

Project: Views (for Drupal 7) » Search API
Version: 8.x-3.x-dev » 8.x-1.x-dev
Component: Code » Views integration
Issue tags: -Search API, -search api solr, -Search API index
drunken monkey’s picture

Which module versions are you using? The latest Alphas from both Search API and Search API Solr, and a recent Drupal Core release? Otherwise, please update and see if that helps.

PS: It seems you (like many others – it's really easy to misinterpret) are confused by the "Issue tags" field. As the guidelines state, they aren't meant for free text tags related to the issue, but only for specific categorization purposes, usually by module maintainers.
So, if you aren't sure your current usage is correct, please just leave the field empty.

danbruder’s picture

I am getting the same thing - here is more information:
Versions:
- Drupal 8.1.7
- Search API 8.x-1.0-alpha16

Steps to reproduce:
1. Configure Search API Database as search backend
2. Create node index that has the path alias as a field
3. Create a view of indexed data
4. Add the indexed path alias field

Error output:

Notice: Undefined index: path in Drupal\views\Plugin\views\field\Field->getFieldStorageDefinition() (line 332 of core/modules/views/src/Plugin/views/field/Field.php).
Drupal\views\Plugin\views\field\Field->getFieldStorageDefinition() (Line: 346)
Drupal\views\Plugin\views\field\Field->defineOptions() (Line: 99)
Drupal\search_api\Plugin\views\field\SearchApiEntityField->defineOptions() (Line: 138)
Drupal\views\Plugin\views\PluginBase->init(Object, Object, Array) (Line: 102)
Drupal\views\Plugin\views\HandlerBase->init(Object, Object, Array) (Line: 108)
Drupal\views\Plugin\views\field\FieldPluginBase->init(Object, Object, Array) (Line: 177)
Drupal\views\Plugin\views\field\Field->init(Object, Object, Array) (Line: 57)
Drupal\search_api\Plugin\views\field\SearchApiEntityField->init(Object, Object, Array) (Line: 888)
Drupal\views\Plugin\views\display\DisplayPluginBase->getHandlers('field') (Line: 2407)
Drupal\views\Plugin\views\display\DisplayPluginBase->validate() (Line: 2097)
Drupal\views\ViewExecutable->validate() (Line: 125)
Drupal\views_ui\ViewFormBase->getDisplayTabs(Object) (Line: 49)
Drupal\views_ui\ViewFormBase->prepareEntity() (Line: 134)
Drupal\Core\Entity\EntityForm->init(Object) (Line: 26)
Drupal\views_ui\ViewFormBase->init(Object) (Line: 105)
Drupal\Core\Entity\EntityForm->buildForm(Array, Object) (Line: 41)
Drupal\views_ui\ViewFormBase->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 512)
Drupal\Core\Form\FormBuilder->retrieveForm('view_edit_form', Object) (Line: 271)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 48)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object, 'edit', Array) (Line: 226)
Drupal\views_ui\Controller\ViewsUIController->edit(Object, NULL)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 98)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 50)
Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
drunken monkey’s picture

2. Create node index that has the path alias as a field

For the path alias, this is a known problem which should have been fixed with #2284199: Problem with taxonomy term parent. Since the path alias field uses a "custom storage", it's (apparently) not possible to access it in a generic way (like we want/have to). The latest Search API versions therefore don't allow indexing the path alias (or, e.g., taxonomy term parents) anymore until the Drupal Core problem is fixed.

legolasbo’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

I am closing this issue because information was requested over a year ago, but no new information has been provided since. Please reopen this issue with the requested information if you think that this issue is still relevant.

niklan’s picture

Status: Closed (cannot reproduce) » Active
StatusFileSize
new6.23 KB

Reopen issue, having the same problem and having the steps for reproducing.

1. Add custom computed field to the node type. For example.

/**
 * Implements hook_entity_bundle_field_info().
 */
function MYMODULE_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  $fields = [];

  if ($entity_type->id() == 'node' && $bundle == 'product') {
    $fields['my_computed_price'] = BaseFieldDefinition::create('float')
      ->setLabel(t('Computed product price'))
      ->setDescription(t('The computed price from all possible sources.'))
      ->setComputed(TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setClass('\Drupal\MYMODULE\Field\ComputedPriceFieldItem');
  }

  return $fields;
}

2. Create ComputedPriceFieldItem.php which referenced in field.

<?php

namespace Drupal\MYMODULE\Field;

use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TraversableTypedDataInterface;
use Drupal\Core\TypedData\TypedDataInterface;

/**
 * Class ComputedPriceFieldItem.
 */
class ComputedPriceFieldItem extends FieldItemList implements TypedDataInterface {

  use ComputedItemListTrait;

  /**
   * Computes the values for an item list.
   */
  protected function computeValue() {
    // Adds static value for example.
    $this->createItem(0, 10.5);
  }

}

3. Add this field to Search API index as the decimal field. My example (the select is "decimal")

search api field

4. Index it.
5. Add for Search API view for that index, you immediately get this error during AJAX modal submit, and after refresh the page, this will be broken and print the same error.

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getType() on null in Drupal\views\Plugin\views\field\EntityField->defineOptions() (line 389 of core/modules/views/src/Plugin/views/field/EntityField.php).

Drupal\views\Plugin\views\field\EntityField->defineOptions() (Line: 99)
Drupal\search_api\Plugin\views\field\SearchApiEntityField->defineOptions() (Line: 138)
Drupal\views\Plugin\views\PluginBase->init(Object, Object, Array) (Line: 102)
Drupal\views\Plugin\views\HandlerBase->init(Object, Object, Array) (Line: 117)
Drupal\views\Plugin\views\field\FieldPluginBase->init(Object, Object, Array) (Line: 217)
Drupal\views\Plugin\views\field\EntityField->init(Object, Object, Array) (Line: 57)
Drupal\search_api\Plugin\views\field\SearchApiEntityField->init(Object, Object, Array) (Line: 895)
Drupal\views\Plugin\views\display\DisplayPluginBase->getHandlers('field') (Line: 2420)
Drupal\views\Plugin\views\display\DisplayPluginBase->validate() (Line: 528)
Drupal\views\Plugin\views\display\PathPluginBase->validate() (Line: 501)
Drupal\views\Plugin\views\display\Page->validate() (Line: 2112)
Drupal\views\ViewExecutable->validate() (Line: 125)
Drupal\views_ui\ViewFormBase->getDisplayTabs(Object) (Line: 49)
Drupal\views_ui\ViewFormBase->prepareEntity() (Line: 136)
Drupal\Core\Entity\EntityForm->init(Object) (Line: 26)
Drupal\views_ui\ViewFormBase->init(Object) (Line: 107)
Drupal\Core\Entity\EntityForm->buildForm(Array, Object) (Line: 41)
Drupal\views_ui\ViewFormBase->buildForm(Array, Object)
call_user_func_array(Array, Array) (Line: 519)
Drupal\Core\Form\FormBuilder->retrieveForm('view_edit_form', Object) (Line: 276)
Drupal\Core\Form\FormBuilder->buildForm('view_edit_form', Object) (Line: 61)
Drupal\Core\Entity\EntityFormBuilder->getForm(Object, 'edit', Array) (Line: 226)
Drupal\views_ui\Controller\ViewsUIController->edit(Object, NULL)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 151)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 42)
Drupal\kmk\Kmkmiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

The problem leads to core Views EntityField::defineOptions()

There is code $field_storage_definition = $this->getFieldStorageDefinition(); which will get NULL for computed field, since core views doesn't supports them and here the mess begins. SInce $field_storage_definition is NULL, so the next line $field_type = $this->fieldTypePluginManager->getDefinition($field_storage_definition->getType()); triggers this error.

I'm not sure if this possible to fix, but I expected Search API views will bridge data from index to views frought own field by search api data type.

drunken monkey’s picture

Priority: Major » Normal
Status: Active » Needs review
StatusFileSize
new1.06 KB

Thanks for re-opening and providing more detailed information!
With this, it was easy for me to reproduce the problem and come up with a sensible solution/workaround. (Just briefly stumbled because you forgot $this->list[0] = in computeValue().)
Please see/test/review the attached patch!

drunken monkey’s picture

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

This looks like a good way of doing this until that core issue gets committed, after that one is in, we can find a way to find a support computed fields properly.

niklan’s picture

+1 for RTBC

Looking forward to core support for computed fields.

niklan’s picture

Status: Reviewed & tested by the community » Needs work

Change status to "Needs works" since it possible can be BC change.

I'll test it later.

Actually, Views do support computed fields and properties since 8.4. But they need to be more work done.

I want to test it, what happens if I do all of those requirements for Views and my computed field. If it will work, possible that some people use this approach and this patch will break their views and other stuff, since it force block computed fields to be used.

niklan’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new22.22 KB

Additionally to computed field definition I applied

/**
 * Implements hook_views_data_alter().
 */
function MYMODULE_views_data_alter(array &$data) {
  if (isset($data['node'])) {
    $data['node']['price'] = [
      'title' => t('Width'),
      'field' => [
        'id' => 'MYMODULE_price',
      ],
    ];
  }
}

and created Views Field Plugin

<?php

namespace Drupal\MYMODULE\Plugin\views\field;

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

/**
 * A handler to provide proper displays for width.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("MYMODULE_price")
 */
class Price extends FieldPluginBase {

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

  /**
   * {@inheritdoc}
   */
  public function query() {
    // This function exists to override parent query function.
    // Do nothing.
  }

}

The error still remains the same.

After applying the patch #10 the error has gone and I see values for both fields Views Plugin Field and Index Source.

Views Field Plugin doesn't affect the result at all when I tried to completely remove the hook and the field plugin. And the behavior remains the same (that's odd but not related to this issue).

Conclusion: This patch is safe to apply even if there are Views Filed Plugin and a corresponding hook, they are not affect computed fields at all and patch works in both cases the same. The result for both fields got from the search index.

Example

  • drunken monkey committed a928916 on 8.x-1.x
    Issue #2674750 by drunken monkey, Niklan, borisson_: Fixed display of...
drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Alright, thanks for investigating!
As per the comment in \Drupal\views\Plugin\views\field\EntityField::getFieldStorageDefinition() I think Core might have only fixed this for computed base fields, not all computed fields. Not sure, though.

Anyways, if this is needed and approved, let’s commit it.
Thanks again, everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.