Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If an Entity Reference field has no value, it isn't listed in a GET and gives an error on a PATCH #405

Open
BurningDog opened this issue Feb 5, 2015 · 4 comments

Comments

@BurningDog
Copy link

I'm trying to update the value of a field in drupal with a PATCH request and getting the following error message:

detail: "Bad Request."
status: 400
title: "Property current_course cannot be set."
type: "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1"

Field current_course is an Entity Reference field which lists the groups a user belongs to. I have defined it in function publicFieldsInfo() as:

    $public_fields['current_course'] = array(
      'property' => 'field_current_course',
      'sub_property' => 'nid',
    );

When the field doesn't have a value and I perform a GET on the API endpoint, current_course doesn't show up at all in the returned JSON. I expect to see at least "current_course":null

However, once the field has a value, the PATCH request works as expected.

@BurningDog
Copy link
Author

A similar problem: if a drupal field doesn't have a value then no process_callbacks run.

@martincollar
Copy link

Hi @BurningDog I'm just solving same thing, found some walkaround:

Add viewEntity to your resource class:

  public function viewEntity($id) {
    $values = parent::viewEntity($id);

    foreach ($this->getPublicFields() as $public_field_name => $info) {

      if (isset($info["default_value"])
        && isset($info["default_value_callback"])
        && array_key_exists($public_field_name, $values)
        && $info["default_value"] === $values[$public_field_name]
      ) {
        $values[$public_field_name] = static::executeCallback($info["default_value_callback"]);
      }
    }

    return $values;
}

I know for example if uid is 0 than there is FALSE in response, so just add new keys to publicFieldsInfo:

$public_fields['author'] = array(
  'property' => 'uid',
  'process_callbacks' => array(
    '_mymodule_author_callback',
  ),
  'default_value' => FALSE,
  'default_value_callback' => '_mymodule_author_default_callback',
);

What do you think about this?

@rw355
Copy link

rw355 commented May 17, 2015

Having this problem when do POST. It seems a problem with entity module though.

global $user;
$values = array('type' => 'agreement');
$entity = entity_create('node', $values);
$wrapper = entity_metadata_wrapper('node', $entity);
$access = $wrapper->field_entity_ref->access('view', $user);
dvm($access); // this is always false

@chasingmaxwell
Copy link

I ran into this issue as well. I've created an issue on the entity module's queue and uploaded a patch which is working for me.

https://www.drupal.org/node/2709457

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants