www.ikonami.net
Ikonami organized a #drupalMeetup in #Islamabad
Date: August 19, 2015
Presentation Topic: Drupal 8 Overview
Presenter: Asghar Khan
Agenda:
What is Drupal 8? in a line
Drupal 8 new core modules.
Modules removed from core.
Drupal 8 directory Structure.
Drupal 8 module development.
Drupal 8 module routing system
Drupal 8 form API
Drupal 8 Plugin system
Open Discussion:
One-on-One sessions with programmers, who are facing any issue in Drupal 7 customization OR stuck in coding of modules.
2. Muhammad Asghar Khan
Drupal Consultant - Ikonami
• Drupal Developer
• 7 years experience in IT
• Zend Certified PHP Eng.
• Drupal Module contributor
Speaker
August 19, 2015 www.ikonami.net
3. Agenda
• What is Drupal 8 in a line?
• Drupal 8 new core modules.
• Modules removed from core.
• Drupal 8 directory Structure.
• Drupal 8 module development.
• Drupal 8 module routing system
• Drupal 8 form API
• Drupal 8 Plugin system
August 19, 2015 www.ikonami.net
4. What is Drupal 8 in a line?
D8 = D7 + D7 contrib modules
D8 build fairly sophisticated sites without having
to install 30+ contributed modules as we did in
Drupal 7.
August 19, 2015 www.ikonami.net
5. Drupal 8 - New Core Modules
• Actions
• Ban
• Basic Authentication
• Block Content
• Breakpoint
• CKEditor
• Config
• Config Translation
• Content Translation
• Datetime
• Editor
• Entity Reference
• HAL
• History
• Language
• Link
• Menu Link Content
• Menu UI
• Migrate
• Migrate Drupal
• Options
• Quickedit
• Responsive Image
• Rest
• Serialization
• Telephone
• Text
• Tour
• Views
• Views UI
August 19, 2015 www.ikonami.net
6. Modules Removed from Core
• Blog
• Dashboard
• Menu
• Open ID
• Overlay
• PHP
• Poll
• Profile
• Translation
• Trigger
August 19, 2015 www.ikonami.net
7. Drupal 8 - Directory Structure
August 19, 2015 www.ikonami.net
8. Drupal 8 - Directory Structure
1. /core - Drupal core files like script, misc, themes etc.
2. /libraries - 3rd party libraries, i.e. “wysiwyg editor”
3. /modules – To place contributed and custom modules
4. /profile - contributed and custom profiles
5. /themes - contributed and custom (sub)themes
6. sites/[domain OR default]/{modules, themes} - Site
specific modules and themes can be moved into these
directories to avoid them showing up on every site
7. sites/[domain OR default]/files - Files directory
August 19, 2015 www.ikonami.net
9. Basic Module Files
Only “.info.yml” file is required for Drupal 8.
× In Drupal 8, we don’t need “.module file”
August 19, 2015 www.ikonami.net
10. Drupal 8 - .info.yml file
Drupal 8 introduced new info file for component i.e.
“.info.yml”. So all .info files are moved to .info.yml
files. The new file extension is .info.yml.
This applies to
Modules
Themes
Profiles
August 19, 2015 www.ikonami.net
11. .info.yml syntax
In .info file we used equal(=) for assigning and square
brackets([]) for array. In .info.yml we will use colon(:) for
assigning and start space and dash for array.
For the most part, change all = to :.
For arrays (e.g. dependencies[] = node), use the following
format in Drupal 8:
dependencies:
- node
In Drupal 7 we use ; for Comments BUT in Drupal 8 we will
use # for Comments.
August 19, 2015 www.ikonami.net
12. Modified entries in .info.yml File
• New Required Element type
–A new type key is now required with values that indicate the type
of extension. Use module, theme or profile. For example:
–type: module
• Remove files[] entries
–Remove any files[] entries. Classes are now autoloaded.
• Convert configure links to route names
–In Drupal 8, specify the administrative configuration link using the
route name instead of the system path.
• Drupal 7
–configure = admin/config/system/actions
• Drupal 8
–configure: action.admin
August 19, 2015 www.ikonami.net
13. Drupal 7 - Theme info
August 19, 2015 www.ikonami.net
14. Drupal 8 - .info.yml
August 19, 2015 www.ikonami.net
15. Drupal 8 - Module .inf.yml
August 19, 2015 www.ikonami.net
name: My test Module
type: module
description: My first demo module to test my development approach.
package: Web services
version: VERSION
core:8.x
dependencies:
-rest
-serialization
16. Create your “First module”
Create your info file in your module directory
/modules/hello_d8/hello_d8.yml.
name: Hello D8
description: Demonstrate Drupal 8 module development.
type: module
core: 8.x
August 19, 2015 www.ikonami.net
17. Create Menu
• In drupal 8 “hook_menu” has been removed and introduced
Routing-Approach.
• Create routing file like [your_module].routing.yml
/modules/hello_d8/hello_d8.routing.yml
hello_d8.page:
path: /hello-d8/page
defaults:
_controller: 'Drupalhello_d8ControllerHelloD8Controller::pageCallback’
_title: 'Hello Drupal 8’
Requirements:
_permission: 'access content'
August 19, 2015 www.ikonami.net
18. Create your module controller
As Drupal 8 follows PSR-4 folder structure so you need to create your
controller file under
/modules/hello_d8/src/Controller/HelloD8Controller.php
<?php
namespace Drupalhello_d8Controller;
use DrupalCoreControllerControllerBase;
class HelloD8Controller extends ControllerBase {
public function pageCallback () {
return [
'#markup' => $this->t('Welcome Drupal 8 uesers')
];
}
}
August 19, 2015 www.ikonami.net
20. Create your form in Drupal 8
Add your page path in hello_d8.routing.yml file.
/modules/hello_d8/hello_d8.routing.yml
hello_d8.config:
path: /admin/config/system/hello-d8-config
defaults:
_form: 'Drupalhello_d8FormConfigForm'
_title: 'Drupal 8 Configuration'
requirements:
_permission: 'configure_hello_d8'
August 19, 2015 www.ikonami.net
21. Create your form in Drupal 8
Create your form class to build your form.
<?php
namespace Drupalhello_d8Form;
use DrupalCoreFormConfigFormBase;
use DrupalCoreFormFormStateInterface;
class ConfigForm extends ConfigFormBase {
protected function getEditableConfigNames() {
return ['hello_d8.settings'];
}
public function getFormId() {
return 'hello_d8_config';
}
function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('hello_d8.settings');
$form['default_count'] = [
'#type' => 'number',
'#title' => $this->t('Default count'),
'#default_value' => $config->get('default_count'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$config = $this->config('hello_d8.settings');
$config->set('default_count', $form_state->getValue('default_count'));
$config->save();
}
}
August 19, 2015 www.ikonami.net
22. Drupal 8 - Form Page
August 19, 2015 www.ikonami.net
23. Drupal 8 - Plugin API
Plugins are small pieces of functionality that are
swappable. Plugins that perform similar
functionality are of the same plugin type.
August 19, 2015 www.ikonami.net
24. Drupal 8 - Block Code
Create your plugin class /modules/hello_d8/src/Plugin/Block/HelloD8Block.php
<?php
namespace Drupalhello_d8PluginBlock;
use DrupalCoreBlockBlockBase;
/**
* Create hello_d8 block.
* @Block(
* id = "hello_d8_block",
* admin_label = @Translation("Hello Drupal 8"),
* category = @Translation("System")
* )
*
*/
class HelloD8Block extends BlockBase {
public function build() {
return [
'#markup' => $this->t('Hello Drupal 8 block.')
];
}
}
August 19, 2015 www.ikonami.net