WSCCI Conversion Guide - Pass 2

Dig deeper into our new controller class

So far it doesn't seem like we've gotten much out of this work. The class will now lazy load for us, but that's about it. Where it becomes useful is dependencies.

A dependency is some other object or code that our code leverages. There are two kinds of dependencies: Hard dependencies are those that are hard coded into our code and cannot be changed without rewriting. Injected dependencies are objects that we pass into our object explicitly, which means we can change them by passing in different objects that have the same interface. That makes code more cleanly separated, makes it clearer what dependencies we have, and makes testing far easier.

There are lots of ways to go about separating and injecting dependencies. For now, we'll show one example: both of our new controller methods call book_get_books(), making that a hard dependency. Instead of having a random function floating around that we cannot test, let's convert that into a service. A service is simply any object that 1) is managed by the service container and 2) gets its own dependencies from the service container. Otherwise there's nothing special about them.

WSCCI Conversion Guide - Pass 1

1. Leverage the new routing system

First, we need to switch the declaration of the routes (the mapping rules) to the new routing system. The new routing system doesn't use a hook but a YAML file, book.routing.yml, that is placed in the root of the module directory.

book.routing.yml

book.render:
  path: '/book'
  defaults:
    _content: '\Drupal\book\Controller\BookController::bookRender'
  requirements:
    _permission: 'access content'

book.admin:
  path: '/admin/content/book'
  defaults:
    _content: '\Drupal\book\Controller\BookController::adminOverview'
  requirements:
    _permission: 'administer book outlines'

There's a lot going on here, so let's take it one step at a time.

Every route has a machine name. By default, using the name of the page callback function we're replacing is usually fine. Then there are three key sections for each route.

path
This is the path, what would have gone in the $items array as a key in hook_menu. Note that the path must begin with a / and may consist of not more than 9 parts each separated by a /. Also note that any placeholders are marked with {}, rather than %. More on that later.

WSCCI Conversion Guide

This guide is a basic tutorial for the process of converting legacy page callbacks to new-style controllers. It is not a guide for porting from Drupal 7 to Drupal 8; it assumes that you're working with a fairly recent copy of Drupal 8 HEAD. (A contrib module guide for porting from Drupal 7 to Drupal 8 can be based off of this document later.)

We will demonstrate the process by looking at the Book module in core. These are real-live examples (based on a true story!) of a core callback getting converted. Some snippets have been reformatted or tweaked to help illustrate a point. There are a lot of moving parts, so we'll take it in several passes and build up our understanding of what is going on. (This example taken directly from this issue.)

If you want to help with the conversion (please do!), see any issue with the WSCCI-Conversion tag or stop by #Drupal-WSCCI in IRC.

Forms not appearing

Hi,

I am in need of help. I did everything by the book, and yet one of my two forms doesn't appear and I'm not getting any tabs either. Manually browsing to the not-appearing form shows it. Non-problematic form is at http://drupalcode.org/project/tcmb.git/blob/3cb17d2:/lib/Drupal/tcmb/Tcm... and problematic one http://drupalcode.org/project/tcmb.git/blob/3cb17d2:/lib/Drupal/tcmb/Tcm.... Here is my routing.yml: http://drupalcode.org/project/tcmb.git/blob/3cb17d2:/tcmb.routing.yml. And just in case, my hook_menu implementation looks like

<?php
function tcmb_menu() {
$items['admin/config/system/tcmb-settings'] = array(
'title' => 'Tcmb General Settings',
'description' => 'Configure Tcmb module',
'route_name' => 'tcmb_currency_settings',
'weight' => 1,
);
$items['admin/config/system/tcmb-settings/currency-settings'] = array(
'title' => 'Currency settings',
'description' => 'Configure Tcmb currency settings',
'route_name' => 'tcmb_currency_settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
);
$items['admin/config/system/tcmb-settings/gold-settings'] = array(
'title' => 'Gold settings',
'description' => 'Configure Tcmb gold settings.',
'route_name' => 'tcmb_gold_settings',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);

Upgrading D8 Nightly Builds

Hello all,

I've been using Drupal for some time now, and with building experience I'm finding myself starting websites from scratch to be quite regular :)

I'm just curious,

I understand D8 is unstable, but if I was building a site in D8 at the current time to be released after Drupal 8's official release, will I encounter any problems upgrading? I'm thinking more about the core improvements which change over time.
I'd like to build all I can when I can at the various stages of D8's development cycle, but I need assurance I will be able to pull it off.

drupal with windows and sql server

Hi

I am new to drupal. I want to build site with following features. would you please guide me if i can use drupal or not?

Pages

Subscribe with RSS Subscribe to RSS - Drupal 8.x