RESTful-Drupal / restful

RESTful best practices for Drupal
https://drupal.org/project/restful
419 stars 173 forks source link

Fatal error with a field collection that is a list of values #996

Closed andrewrutter closed 7 years ago

andrewrutter commented 7 years ago

I think my issue is related to https://github.com/RESTful-Drupal/restful/issues/829 but I am not sure how to resolve based on the notes on that issue.

In my Drupal configuration I have the following for a content type of Quiz which has a field collection which is called field_questions. This field collection also has a field of type field_collection called answers. Both of these nested field collections are setup to be unlimited lists.

Quiz->[field_questions->[field_answers]]

I am trying to expose this structure through RESTful and so have the following class for my Question resource in a file src/Plugin/resource/question/Question__1_0.php

<?php

/**
 * @file
 * Contains \Drupal\restful_example\Plugin\resource\question\Questions__1_0.
 */

namespace Drupal\restful_example\Plugin\resource\question;

use Doctrine\Common\Collections\ArrayCollection;
use Drupal\restful\Plugin\resource\Field\ResourceFieldCollection;
use Drupal\restful\Plugin\resource\ResourceEntity;

/**
 * Class Questions__1_0
 * @package Drupal\restful_tutorial\Plugin\resource\question
 *
 * @Resource(
 *   name = "questions:1.0",
 *   resource = "questions",
 *   label = "Questions",
 *   description = "Export the questions collection with all authentication providers.",
 *   authenticationTypes = TRUE,
 *   authenticationOptional = TRUE,
 *   dataProvider = {
 *     "entityType": "field_collection",
 *     "bundles": {
 *       "field_questions"
 *     },
 *   },
 *   majorVersion = 1,
 *   minorVersion = 0
 * )
 */
class Questions__1_0 extends ResourceEntity {

  /**
   * {@inheritdoc}
   */
  protected function publicFields() {
    $public_fields = parent::publicFields();

    $public_fields['title'] = array(
      'property' => 'field_heading'
    );

    return $public_fields;
  }

}

If I try to clear Drupal cache or access my /api endpoint I get the following fatal error:

Fatal error: Call to undefined method EntityValueWrapper::entityKey() in sites/all/modules/restful/src/Plugin/resource/Field/ResourceFieldEntity.php on line 1166

I have tried various ways of representing this resource but so far nothing seems to work and the solution given on the above tickets is not clear to me. Thanks!

andrewrutter commented 7 years ago

I was misreading the ticket above and realize that the developer was referring to making a change to the Entity module and not RESTful. This does work and I guess is something that would be good to share with that team?

OnGitHubSchatz commented 6 years ago

I've been running into the same error message at times while fiddling with the classes being extended in my resources, even when returning to last good state with version control.

Have your class extend something unrelated like class Questions__1_0 extends ResourceFieldEntityReference, clear cache, return it to ResourceEntity and then clear cache once more. Not sure why this occurs, something weird persisting when autoloading these?

e0ipso commented 6 years ago

@gitschatz thanks for the recipe. You can also use the https://www.drupal.org/project/registry_rebuild technique for that. The autoloader project we are using leverages caching for performance reasons, that's why the extra steps are needed.

OnGitHubSchatz commented 6 years ago

@e0ipso Cheers! That's a nifty command it adds.