Created a post on using v8 and angular with each being on separate sites

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
RobKoberg's picture

http://www.koberg.com/2014/06/08/headless-drupal-8-and-angularjs-theme-s...

However, I got stuck with the article's image and term data. I created a support issue here: https://drupal.org/node/2282603 but any advice would be appreciated.

Comments

Amazing! Truly, great work

jessebeach's picture

Amazing! Truly, great work and intense detail.

Might you have any interest in putting this work into a D8 sandbox that we could start working on together?

headless drupal

RobKoberg's picture

Thanks for taking a look and the compliments!

I am not familiar with the D8 sandbox (a search didn't turn anything up). I have seen some sandbox projects, but not sure this would be a project, per se. I just set up a vanilla D8 install and modified (read: quick hack) the drupal $DOCROOT/.htaccess to allow Cross-Origin Resource Sharing. Then I added an article node with a populated field_image and tags taxonomy term. I mostly wanted to show how to use angular as a front end site completely separate from drupal.

The angularjs site/client-app might be something for a sandbox/project. Is that what you mean? But is not really a project you would install into drupal as it is a complete separation. I do have a github repo at https://github.com/rkoberg/d8client. I had planned on flushing out more detail for the angular section, but ran out of time over the weekend after running into problems. I created an issue here, with the relevant response HAL+JSON - https://drupal.org/node/2282603

I ran into problems where the JSON response was not coming with any useful image data (even to hack a public:// uri). I would have expected that to come back with the configured display image style URL, but there wasn't even the file entity reference information. The taxonomy term did come down with the entity reference information, but that would require me to make another call back to the drupal site to get the term name. When trying out restws with D7, I had to do alter hooks (if I remember correctly) to get the necessary entity references to be populated for a response. Do you know if that is the intention for D8? Perhaps a request parameter needs to sent? Or does a dev type need to write code for this? I couldn't find any information about how to handle this type of thing (in any way) after quite a bit of looking. I have not had to look into the code too deeply (it will also be a learning experience). I am worried that the entity references will need to resolved at a high level.

I am very excited about a 'headless drupal' -- one where I can throw up a quick drupal somewhere. Then have designer types be able to GET JSON and have their way with it based on a base set of angularjs controllers, services, directives, etc, and templates. I think this would be a boon to Drupal where they would not need any knowledge of it. But developers and configurers can do what they need without worrying about making it look good. The drupal I am thinking of would boot to the DATABASE level (or possibly to SESSION) for requests except those starting with /admin. I have done this with a D7 site and an angular front end, except bootstrapping to lower levels. I tried to use the https://drupal.org/project/js module to help with the bootstrapping switch when needed, but found I almost always had to do a full bootstrap, and the project needed to get done... That is probably enough for now :)

Anyway, let me know what you mean by sandboxing -- I don't have a problem with that.

Yes, you have to do

moshe weitzman's picture

Yes, you have to do additional calls for referenced entities like taxo terms. Contrib may be able to improve this.

My recollection is that imagefields and fielfields should have data returned in a GET request. We never finished write support for those - https://drupal.org/node/1927648.

well that's a buzzwrecker.

RobKoberg's picture

well that's a buzzwrecker. Then there is no such thing as a "Headless Drupal" out of the box. It would seem to be a basic core requirement.

Perhaps a call to node/1 would work as it currently does, but a GET to node/1/full or node/1/teaser could populate the entity references with the same things that the templated view would get (but without any theming). That is, a field_image would come with the URL to converted image style for a particular display.

Anyway, could you (or anyone) point me to what needs to happen to populate the entity references. Is the best place a hook_node_alter (if there still is this hook) or do we implement/extend a class? Ideally I would like to do this at a low bootstrap level like DATABASE (if that is still the name). I have to do this work in my free time and any help would be great.

Thanks!

stevector's picture

Thanks so much for this post. It got me to finally install node et al and I was able to get https://github.com/rkoberg/d8client/tree/develop running and reading from Drupal 8. I agree that the REST response needs a lot more info.

I think there are at least two main ways of going about getting the response you want.

  1. Add/override the response: hook_rest_resource_alter() seems like a starting point for changing what happens to that request. But I think that hook is meant more for a contrib or custom site use case. And you're looking (rightly) to make this concept work better out of the box. I think the link Moshe posted above is a good place to start.

  2. Use Views Module to craft the response you want: See http://drupalize.me/blog/201402/your-first-restful-view-drupal-8 I'm reluctant to use Views as the golden hammer to solve all problems but it does provide a UI for building up exactly what you want the server to send back.