Error appears when Aggregation is used and trying to use aggregation on field with reference (entity reference) to taxonomy entity.

Fatal error: __clone method called on non-object in /core/modules/views/src/Plugin/views/field/Field.php on line 855

Because of some reasons in protected function createEntityForGroupBy(EntityInterface $entity, ResultRow $row) $entity == NULL.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sinn created an issue. See original summary.

sinn’s picture

Issue summary: View changes
sinn’s picture

Patch is attached

Status: Needs review » Needs work

The last submitted patch, 3: views-clone_method_called_on_non_object-2772711-3-D8.patch, failed testing.

ozin’s picture

Status: Needs work » Needs review
FileSize
1.38 KB

Hi @sinn, great work! Just fixed patch paths.

sinn’s picture

Actually patch #3 doesn't resolve error:

The website encountered an unexpected error. Please try again later.
Recoverable fatal error: Argument 1 passed to Drupal\views\Plugin\views\field\Field::createEntityForGroupBy() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /views/field/Field.php on line 809 and defined in Drupal\views\Plugin\views\field\Field->createEntityForGroupBy() (line 851 of core/modules/views/src/Plugin/views/field/Field.php).

New patch is attached.

Status: Needs review » Needs work

The last submitted patch, 6: views-clone_method_called_on_non_object-2772711-6-D8.diff, failed testing.

sinn’s picture

Sorry, updated paths in a patch

Status: Needs review » Needs work

The last submitted patch, 8: views-clone_method_called_on_non_object-2772711-7-D8.diff, failed testing.

sinn’s picture

Status: Needs work » Needs review
FileSize
926 bytes

Updated

Lendude’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Since NULL is a valid return value for getEntity, adding a check for it sounds good.

+++ b/core/modules/views/src/Plugin/views/field/Field.php
@@ -806,7 +806,13 @@ public function getItems(ResultRow $values) {
+      if (is_object($original_entity))  {

getEntity either returns an object implementing \Drupal\Core\Entity\EntityInterface or null, so i'd just do if ($original_entity) { , either way you know you are dealing with one or the other.

Also, this needs tests.

dawehner’s picture

+++ b/core/modules/views/src/Plugin/views/field/Field.php
@@ -806,7 +806,13 @@ public function getItems(ResultRow $values) {
+      $original_entity = $this->getEntity($values);
+      if (is_object($original_entity))  {
+        $entity = $this->createEntityForGroupBy($original_entity, $values);
+      }
+      else {
+        $entity = NULL;
+      }

According to the documentation of getEntity we return EntityInterface|null, so we could simplify this a bit more here: if ($entity = $this->getEntity($values)) { $entity = $this-> ... and skip the else completely.

subson’s picture

Status: Needs work » Needs review
FileSize
822 bytes

based on dawehner comments, trying another patch.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

das-peter’s picture

I think this is obsolete for 8.3.x as following code is used in Drupal\views\Plugin\views\field\EntityField (successor of Drupal\views\Plugin\views\field\Field):

      // Optional relationships may not provide an entity at all. So we can't
      // use createEntityForGroupBy() for those rows.
      if ($entity = $this->getEntity($values)) {
        $entity = $this->createEntityForGroupBy($entity, $values);
        // Some bundles might not have a specific field, in which case the faked
        // entity doesn't have it either.
        $build_list = isset($entity->{$this->definition['field_name']}) ? $entity->{$this->definition['field_name']}->view($display) : NULL;
      }
      else {
        $build_list = NULL;
      }
kristiaanvandeneynde’s picture

Status: Needs review » Closed (outdated)