Skip to content

Post new entry in Entity List with Node Reference to other Node #1061

@charlie-rushton

Description

@charlie-rushton

Hi,

I am trying to submit a post request to my resource containing a node reference to another node in my drupal site.

My resource is a log file where I can submit entries with a type and a description, and this has a node reference field which should be referencing a student.

I can use the restful drupal module to post a description and type (these are just text fields) and the node reference field is set to autocomplete, therefore I am trying to pass a node id and it should autocomplete the rest of the reference.

Below is a code snippet of my resource:

<?php

/**
 * @file
 * Contains \Drupal\restful\Plugin\resource\GustLog__1_0.
 */

namespace Drupal\restful\Plugin\resource;

/**
 * Class Gust Log
 * @package Drupal\restful\Plugin\resource
 *
 * @Resource(
 *   name = "gustlog:1.0",
 *   resource = "gustlog",
 *   label = "Gust Log",
 *   description = "Export the Gust Log entity.",
 *   authenticationTypes = TRUE,
 *   authenticationOptional = FALSE,
 *   dataProvider = {
 *     "entityType": "gust_log",
 *     "bundles": {
 *         "gust_log"
 *     },
 *   },
 *   majorVersion = 1,
 *   minorVersion = 0
 * )
 */
class GustLog__1_0 extends ResourceEntity implements ResourceInterface {

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

        // The mail will be shown only to the own user or privileged users.
        $public_fields['type'] = array(
        'property' => 'field_type',
        );

        $public_fields['description'] = array(
            'property' => 'field_task_description',
        );

        $public_fields['nodeReference'] = array(
            'property' => 'field_node_references'
        );


        return $public_fields;
    }
}

Below is a screenshot of my postman requests. I have tried several different formats for the value of node reference, but it never creates the reference, only a blank array.

Screenshot 2019-11-05 at 13 35 25

P.S. This is a project for a client who already had an installation of drupal and wanted a mobile app to interact with the site, I have very little experience with drupal prior to this and no experience with the RESTful module, so any help will be greatly appreciated. Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions