It's possible I'm missing something obvious, but is there a way to have services views return the field value with the right type? It seems to me that no matter what I do the response value is always a string even when the data is an integer or list (as is the case with a select list of references). For example I would like to get nid as integer rather than a quoted number. I tried all the formatters but all return some type of string. I'm hoping I don't have to write my own formatter, but if I do I wonder if there are any recommendations or suggestions.
Thanks much

Comments

cybertoast’s picture

It turns out my understanding of implementing the services module was quite wrong and what I have been doing so far is using the Page display-type and assuming it could be rendered as a service. I actually thing this needs better documentation, which I'm going to submit, but for now this is my updated understanding in case anyone else has this confusion in future.

Services_Views exposes a new "Services" display-type in your view. This resource requires a path, which is appended to your Services Server's endpoint. Say your server is REST running at "/api", the path for your services-views display end-point would be appended to this. So setting path of "my_view" would have a resource that is retrieved via "/api/my_view". Keep in mind that each resource MUST be enabled in your services resources list at /admin/structure/services/list/rest_api/resources after you have defined your Service-View.

If you want fields to be returned in the original entity format, set the Formatter to "Services Raw".

[
    {
        title: "Case B",
        nid: "7",
        // If you don't set the "skip-safe-value" option in the field confiuration, 
        //  the field value will be separated into value and safe-value.
        Age_Range: {
            value: "Age_Range",
            safe_value: "Age_Range"
        },
        // The "Body" field will provide the value, summary and text-format
        //   If you want to separate the values out, just use the default formatter
        Body: {
            value: "<strong>Case B Lorem Ipsum</strong> <em>Case B Lorem Ipsum<em> Case B Lorem Ipsum <ul><li>Case B Lorem Ipsum</li><li>Case B Lorem Ipsum</li></ul> Case B Lorem Ipsum Case B Lorem Ipsum Case B Lorem Ipsum",
            summary: "Summary to the lorem",
            format: "filtered_html"
        },
        // Use the "Image URL" formatter to just get the full image path
        Photo_thumb: [
            "http://localhost/sites/default/files/styles/thumbnail/public/casestudy_primary_constructionkids.png",
            "http://localhost/sites/default/files/styles/thumbnail/public/casestudy_primary_scigames.png",
            "http://localhost/sites/default/files/styles/thumbnail/public/casestudy_primary_designlab.png",
            "http://localhost/sites/default/files/styles/thumbnail/public/casestudy_primary_thinkyoungmakers.png"
        ],
        // Use the "Services Raw" formatter to get full node details,
        //  but keep in mind that paths will be incorrect
        Photos_carousel: [
            {
                fid: "24",
                alt: "",
                title: "",
                width: "800",
                height: "600",
                uid: "1",
                filename: "casestudy_primary_constructionkids.png",
                uri: "public://casestudy_primary_constructionkids.png",
                filemime: "image/png",
                filesize: "491049",
                status: "1",
                timestamp: "1356125793",
                rdf_mapping: [ ]
            },
            {
                fid: "25",
                alt: "",
                title: "",
                width: "800",
                height: "600",
                uid: "1",
                filename: "casestudy_primary_scigames.png",
                uri: "public://casestudy_primary_scigames.png",
                filemime: "image/png",
                filesize: "619846",
                status: "1",
                timestamp: "1356125793",
                rdf_mapping: [ ]
            },
            {
                fid: "26",
                alt: "",
                title: "",
                width: "800",
                height: "600",
                uid: "1",
                filename: "casestudy_primary_designlab.png",
                uri: "public://casestudy_primary_designlab.png",
                filemime: "image/png",
                filesize: "459412",
                status: "1",
                timestamp: "1356125861",
                rdf_mapping: [ ]
            },
            {
                fid: "27",
                alt: "",
                title: "",
                width: "800",
                height: "600",
                uid: "1",
                filename: "casestudy_primary_thinkyoungmakers.png",
                uri: "public://casestudy_primary_thinkyoungmakers.png",
                filemime: "image/png",
                filesize: "358626",
                status: "1",
                timestamp: "1356125861",
                rdf_mapping: [ ]
            }
        ],
        Process: "Process p",
        Summary: "Case B Lorem Summary",
        Key_Concepts: [
            "Pysical Sciences",
            "Data"
        ]
    }
]

Referenced nodes/entities are returned as a list of targets by default:

    Project_members: [
        { target_id: "4" },
        { target_id: "13" }
    ]

Use the "Label" formatter to get the node's title at the very least, which might be more
useful. Enable "link label to the referenced entity" to get the link, which can then
be parsed for the node id in case you want to do something with that.

The best solution might be to go with the "Rendered Entity" formatter and use the Custom View,
which can be set up so that you get the specific fields and formats that you want.

ygerasimov’s picture

Status: Active » Fixed

As another solution to original problem is to use php type field and do type casting there so integer values won't be treated as strings.

Status: Fixed » Closed (fixed)

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

bc’s picture

Status: Closed (fixed) » Active

For "content" row plugin type, I use this to add pseudo-field values and also to apply the correct type to certain fields:

/**
 * Implements hook_node_load().
 */
function MODULENAME_node_load($nodes, $types) {
  if (!in_array('TYPE', $types)) {
    return;
  }

  foreach ($nodes as $node) {
    if ($node->type == 'TYPE') {
      $node->FIELDNAME = (int) WHATEVER_TO_RETURN_FIELD_VALUE($node);
    }
  }
}

I'm still trying to figure out how to do the same for fields row_plugin...

abaier’s picture

Issue summary: View changes

I cannot find "Services Raw" as formatter in the views ui. Could somebody maybe tell me where to activate this option? I am using Services Views 7.x-1.1.

Thanks in advance.