Drupal Development

Joaquín Bravo (@jackbravo)

Install composer

# https://getcomposer.org/download/
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

# https://getcomposer.org/doc/00-intro.md#globally
sudo mv composer.phar /usr/local/bin/composer
composer --version

Create a new project

# edit your local command line php.ini
# to memory_limit = -1

composer create-project drupal-composer/drupal-project:8.x-dev demo-drupal
# demo-drupal is the name of the folder

# if your internet is slow
wget -c https://ftp.drupal.org/files/projects/drupal-8.8.5.tar.gz
mv drupal-8.8.5 demo-drupal

# Finally
cd demo-drupal

Create a git repository

git init
git add .
# drupal already provides a sensible .gitignore file
# by default it ignores all contrib modules and drupal core
git commit -m "Initial commit"

Don't Hack Core!

If you need to patch drupal core or a contrib modules

you can use

https://github.com/cweagans/composer-patches

this modifications live on the composer.json file

We already have dev tools

# in our composer.json file

"require": {
  ...
  "drupal/console": "^1.0.2",
  "drupal/core": "^8.8.0",
  "drush/drush": "^9.7.1 | ^10.0.0",
  "vlucas/phpdotenv": "^4.0",
  ...
},

Install drupal

./vendor/bin/drush site:install --db-url=sqlite://sites/example.com/files/.ht.sqlite
./vendor/bin/drush runserver

Export site configuration

./vendor/bin/drush config:export
git status
git add config
git commit -m "Add site configuration"

Descargar módulos

chmod u+w web/sites/default
composer require drupal/restui

# From web UI or console...
./vendor/bin/drush pm:enable restui basic_auth
./vendor/bin/drush config:export

git status
git diff
git add composer.* config
git commit -m "Enable restuid and basic_auth module"

Enable rest modules

./vendor/bin/drush config:export
git add config
git commit -m "Enable REST GET for content"

From http://127.0.0.1:8888/admin/config/services/rest

Enable GET for "Content" with:

  • json
  • basic_auth

Create test content

./vendor/bin/drupal create:nodes

# https://github.com/jakubroztocil/httpie
sudo apt-get install httpie
brew install httpie
http "http://127.0.0.1:8888/node/1?_format=json"
http 127.0.0.1:8888/node/1 _format==json

Drupal Console is another drupal CLI tool

It is mostly used for development tasks

Drush is used for more general tasks

Create a REST view

http 127.0.0.1:8888/rest/views/articles _format==json
http -a admin:admin 127.0.0.1:8888/rest/views/articles _format==json

Export view

drupal config:export
git status
git add config
git diff --cached
git commit -m "Create /rest/views/articles view"

Enable media library

Check default media types

Enable on WYSIWYG

Enable on WYSIWYG

2. Enable Embed media on Basic HTML

Enable on WYSIWYG

3. Once enabled, configure it just below

Enable on WYSIWYG

4. Drag its button to the active toolbar and SAVE

Enable on WYSIWYG

5. Try it

Create custom module

./vendor/bin/drupal generate:module
  Enter the new module name:
  > Demo REST

./vendor/bin/drupal generate:plugin:rest:resource
  Enter the module name [restui]:
  > demo_rest
  Enter the plugin rest resource name [DefaultRestResource]:
  > ArticlesRestResource
  Enter the plugin rest resource url:
  > rest/articles

Query database

In ArticlesRestResource.php, replace

with code at demo repo

 

Enable and cc

./vendor/bin/drush pm:enable demo_rest

./vendor/bin/drupal rest:enable
  Rest ID [entity:action]:
  > articles_rest_resource

./vendor/bin/drush cache:clear

http -a admin:admin 127.0.0.1:8888/rest/articles _format==json

References

Drupal Development 8.x

By jackbravo

Drupal Development 8.x

  • 187