Drupal 8 Web Services and Context Core (WSCCI) Initiative

Web services and mobile are more important than ever, and with that comes the need to have more flexible page and layout capabilities

– Dries Buytaert

Introduction

The future is a world where content management systems need to output data to many more devices and integrate with more and more systems and services. Drupal 7 (and previous) was optimized for outputting HTML and core ships with an old XML-RPC backend. If Drupal want to be the go-to platform in a world with many different devices and integrated services, it need fundamentally change that.

That is why Dries Buytaert identified the Web Services and Context Core Initiative (WSCCI) as one of the major initiatives for the Drupal 8 cycle. The goal of Drupal 8’s Web Service Initiative is to make Drupal equally good at outputting data as XML, JSON and other non-HTML formats, to expose Drupal’s functionality through a RESTful interface. The initiative’s Drupal group exclaims:

The Web Services and Context Core Initiative (WSCCI) aims to transform Drupal from a first-class CMS to a first-class REST server with a first-class CMS on top of it. To do that, we must give Drupal a unified, powerful context system that will support smarter, context- sensitive, easily cacheable block-centric layouts and non-page responses using a robust unified plugin mechanism.

But what web services means

In a nutshell, a web service is a web site responding to a request not with an HTML page but with data intended for another program to consume. That could be as mundane as an RSS feed, as complex as a headless REST application server, or anything in between.

What is REST

REST is simply a protocol: a set of rules for requesting information from a server using a specific technique. The rules are important because without rules, you can’t achieve any level of standardization. The best way to view a protocol is as if it is a diplomat who is making a request on your behalf from another entity. REST rely on well-established rules that everyone has agreed to abide by in the interest of exchanging information.

What is RESTful?

So what are the requirements for RESTfulness?
REST includes working with machine readable formats and interacting with resources instead of endpoints. However most APIs that call themselves RESTful at this point will also use standardized HTTP methods, though this hasn’t always been the case, and some APIs will still call themselves RESTful without doing this.

How WSCCI make Drupal support the different parts of RESTfulness

With changes made to the core routing system and the introduction of serialization module, Drupal is now format-aware. The system can serialize content entities – such as nodes and comments – to different data formats. It can also deserialize from some data formats back to entities, though not all data formats support deserialization.

Serialization module supports JSON and XML, though the XML support is quite limited without any support for XML features such as namespaces and attributes. Additionally, the core module HAL, referred to earlier, implements the Hypertext Application Language.

My experience knowing the initiative a little further

The organizers of this initiative are:

Getting started

I was looking at the issues and noticed that one of the goal of the initiative is to convert all page callbacks to a new style controller and they actually created a WSCCI Conversion Guide. As you can see most of child issues was solved but there are still some to solve!

Why that conversion is important for the initiative

Answer: The new routing system
A “route” is a single entry for a how to handle a given request. Primarily, it defines the rules that allow the system to map the request to a controller. A “controller” is any PHP callable (function, object, name of a function, closure, etc.) that returns a Response object. “Routing” is the process of taking an incoming request and finding the route that is most appropriate. The new Symfony-based routing system makes that all hook_menu() have to be replaced with a .routing.yml file that looks as the following:

user.logout:
  path: '/user/logout'
  defaults:
    _controller: '\Drupal\user\Controller\UserController::logout'
  requirements:
    _user_is_logged_in: 'TRUE'

As you noticed the new routing system implements callbacks that reference controllers or forms that are declared as classes in a namespace.

My little contribution

At the moment I didn’t do something wonderful helping the initiative (I’m a novice 🙂 ), but to get started I collaborated to close an issue. The issue was about a “bug” where for the block_admin_edit route we use admin/structure/blocks/manage/barik_login/configure instead of just admin/structure/blocks/manage/bartik_login. I re-rolled the test patch that demonstrated the issue and it worked fine using just the second path. So I submitted my patch:

diff --git a/core/modules/block/src/Tests/BlockTest.php b/core/modules/block/src/Tests/BlockTest.php
index 809f289..728ddc5 100644
--- a/core/modules/block/src/Tests/BlockTest.php
+++ b/core/modules/block/src/Tests/BlockTest.php
@@ -436,4 +436,29 @@ public function testUninstallTheme() {
     $this->assertIdentical(NULL, Block::load($block->id()));
   }
 
+  /**
+   * Test block configure menu default local task.
+   */
+  function testBlockConfigure() {
+    $block_name = 'system_powered_by_block';
+    // Create a random title for the block.
+    $title = $this->randomMachineName(8);
+    $machine_name = strtolower($this->randomMachineName(8));
+    // Enable a standard block.
+    $default_theme = \Drupal::config('system.theme')->get('default');
+    $edit = array(
+      'id' => $default_theme . '_' . $machine_name,
+      'region' => 'sidebar_first',
+      'settings[label]' => $title,
+    );
+    $this->drupalPostForm('admin/structure/block/add/' . $block_name . '/' . $default_theme, $edit, t('Save block'));
+    $this->assertText('The block configuration has been saved.', 'Block was saved');
+
+    $this->drupalGet('user');
+    $this->assertText($title, 'Block title was displayed by default.');
+    debug($title);
+    $this->drupalGet('admin/structure/block/manage/' . $default_theme . '_' . $machine_name);
+    $this->assertFieldByName('settings[label]', $title, 'Found block label input on configuration page');
+  }
+
 }

This passed the test:

patch-pass-test

So, I closed the issue and put status as Closed (cannot reproduce)

closed-cannot-reproduce

 

 

Now, it’s to time to try things a little more complex and helpful 🙂

Would you like to join?


Here is its Drupal Group. The official IRC channel #drupal-wscci is there to help anyone who want to contribute and how I mentioned before, they also made a WSCCI Convertion Guide to start helping out to this great labor. Of course, these are the issues that we need to fix

To finish this I would like to invite you to involve in the initiative if you think you can and I know you can. 🙂

Anuncios

Un comentario en “Drupal 8 Web Services and Context Core (WSCCI) Initiative

  1. Pingback: Drupal 8, le Roi est mort : vive le Roi - Le blog de Clever Age

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s