Fast Paced Drupal 8



Fast Paced Drupal 8

0 2


fastpaced_drupal8


On Github emarchak / fastpaced_drupal8

Fast Paced Drupal 8

Speed up with composer, console and services

@emarchak / @myplanetHQ

emarchak.github.io/fastpaced_drupal8 / github.com/emarchak/fastpaced_videos

@myplanetHQ

@emarchak

Why Change for D8?

Speed up your development process Play well with others

Dependencies

Composer Dependency manager for PHP Drupal Console Extends Symfony console for boilerplates and interactions. Guzzle (in core) Guzzle is a PHP HTTP client. Familiarity with Drupal 7 development

Overview

Build using Composer Install using Console Create a Module using Console Export Content Types using Console Create Admin Form using Console Create a Service using Console Import Nodes using Guzzle Include your module using Composer

Witness Me!

1. Build using Composer

Download Drupal into the docroot.

Composer is a really exciting alternative to Drush Make for building sites.

$ composer create-project drupal/drupal docroot 8.0.5
$ cd docroot

Managing patches using composer

For Drupal 8.0.5 only, we'll need to patch core, but any module can be patched.

$ composer require cweagans/composer-patches --no-update
"extra": {...
  "patches": {
    "drupal/drupal": {
      "Combination of --prefer-dist and .gitattributes confuses our vendor test cleanup": "https://www.drupal.org/files/issues/2664274-37.patch"
  }
}

Add to installer paths to let us download modules to ./docroot/modules/contrib

"extra": {...
  "installer-paths": {
    "modules/contrib/{$name}": [
      "type:drupal-module"
    ] 
  }
},

Add Drupal Composer as a repo, so we can download Drupal modules.

"repositories": [ { 
    "type": "composer", 
    "url": "https://packagist.drupal-composer.org"
} ]

Install some Drupal modules!

Admin Toolbar improves the core Drupal toolbar, and Video Embed Field is used in our demo.

$ composer require drupal/admin_toolbar:8.1.14 --no-update
$ composer require drupal/video_embed_field:8.1.0-rc5 --no-update
$ composer update

2. Install using Console

$ drupal site:install
$ drupal module:install admin_toolbar
$ drupal module:install video_embed_field

Move the configuration sync directory outside of the docroot, and add Symfony's new trusted host patterns.

$config_directories['sync'] = '../config/sync';
$settings['trusted_host_patterns'] = [ '^fastpaced.local$' ];
$ drupal config:export

3. Create a Module using Console

Let's create a fast-paced module...

$ drupal generate:module

Enter the new module name:
> Fastpaced videos

... and a speedy content type!

$ drupal generate:entity:bundle

Enter the module name [admin_toolbar]:
> fastpaced_videos

Enter the machine name of your new content type [default]:
> video

$ drupal module:install fastpaced_videos

4. Export Content Types using Console

Log into the site and configure the content type as needed, before you export it.

$ drupal config:export:content:type
> video

Export content type in module as an optional configuration (yes/no) [yes]:
> no

Can we generate some example content? Yes we can.

$ drupal create:nodes

5. Create Admin form using Console

$ drupal generate:form:config

Enter the Form Class name [DefaultForm]:
> ImportSettingsForm    

Do you want to generate a form structure? (yes/no) [yes]:
> y

Type:          number
Input label:   Import Max
Description:   Maximum amount of nodes to import pre cron run
Default value: 10

Type:          textfield
Input label:   Search Terms
Description:   Feed to import from
Default value: macaframa

Update routing file (yes/no) [yes]:
> yes   


Generate a menu link (yes/no) [yes]:
> yes

A title for the menu link [ImportSettingsForm]:
> Fast Paced Import Settings

Menu parent [system.admin_config_system]:
> system.admin_config_services

Export the configuration settings.

$ drupal config:export:single --directory=modules/custom/fastpaced_videos/config/install
  
Configuration type [Simple configuration]:
> system.simple

Configuration name [automated_cron.settings]:
> fastpaced_videos.importsettings

6. Import Nodes using Guzzle

Add fastpaced_videos_cron().

/**
 * @file
 * Contains fastpaced_videos.module.
 */

use Drupal\Core\Url;
use Drupal\Component\Serialization\Json;
/**
 * Implements hook_cron().
 */
function fastpaced_videos_cron() {...}

Some parts of Drupal 8 are still procedural like 7, hook_theme and hook_cron, for example.

Refer to the repo for further changes to fastpaced_videos.module

7. Create a Service using Console

We'll be importing videos using guzzle, so we'll need to create a service to save them.

$ drupal generate:service
Enter the service name [fastpaced_videos.default]:
> fastpaced_videos.import

Enter the Class name [DefaultService]:
> ImportService

Do you want to load services from the container (yes/no) [no]:
> yes

Enter your service [ ]:
> entity.query
> entity_type.manager

Refer to the repo for further changes to fastpaced_videos/src/ImportServiceInterface.php

8. Add your custom module to composer

Add our github repo as a repo, so we can can download our custom module.

"repositories": [... { 
  "type": "git", 
  "url": "https://github.com/emarchak/fastpaced_videos.git"
}]
$ composer require emarchak/fastpaced_videos

Ride eternal, shiny and chrome.

@emarchak / @myplanetHQ

emarchak.github.io/fastpaced_drupal8 / github.com/emarchak/fastpaced_videos