Voici, mis cote à cote 2 modules hello-world en Drupal 7 et Drupal 8 (beta9) qui mettent en œuvre les concepts principaux qu’on retrouve dans la plupart des modules :
- menus et chemins d’accès
- création de block
- création d’un formulaire
- theming
- accès aux « variables »
Comme vous pouvez le constater, les changements sont nombreux, mais au final :
- c’est la même chose écrit différement (pour simplifier : on passe du fonctionnel à de l’objet)
- si on omet les « namespaces », ce n’est pas foncièrement plus verbeux, mais le code est éclaté en 7 fichiers en Drupal8 au lieu de 3
| Drupal7 | Drupal8 |
|---|---|
hello.info |
hello.info.yml |
| name = Hello world description = Minimalist Hello World in Drupal 7 package = Example modules core = 7.x files[] = hello.module |
name: Hello World type: module description: Minimalist Hello World in Drupal 8 package: Example modules core: 8.x |
hello.modulefunction hello_menu() {
|
hello.links.menu.yml |
$items['helloworld'] = array( 'title' => 'Hello world', |
hello.main: title: Hello world route_name: hello.world |
$items['admin/config/content/hello'] = array( 'title' => 'Hello config', |
hello.form: title: Hello config route_name: hello.form |
hello.routing.yml |
|
$items['helloworld'] = array( 'page callback' => '_page_hello_world', 'access callback' => TRUE, |
hello.world:
path: 'helloworld'
defaults:
_controller: '\Drupal\hello\Controller\HelloRouteController::index'
requirements:
_access: 'TRUE'
|
$items['admin/config/content/hello'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('hello_config_form'),
'access arguments' => array('access hello content')
|
hello.form:
path: 'admin/config/content/hello'
defaults:
_form: '\Drupal\hello\Form\HelloForm'
requirements:
_permission: 'access hello content'
|
function hello_theme() {
return array(
'hello_text' => array(
'template' => 'hello-text',
'variables' => array('text' => NULL)),
);
}
|
function hello_theme() {
return array(
'hello_text' => array(
'template' => 'hello-text',
'variables' => array('text' => NULL)),
);
}
|
src/Controller/HelloRouteController.php |
|
function _page_hello_world() {
return array( '#markup' => '<p>Hello world page text</p>' );
}
|
namespace Drupal\hello\Controller;
use Drupal\Core\Controller\ControllerBase;
class HelloRouteController extends ControllerBase {
public function index() {
return array('#markup' => '<p>Hello world page text</p>');
}
}
|
src/Plugin/Block/HelloBlock.php |
|
function hello_block_info() {
$blocks['hello'] = array(
'info' => t('Hello world block title'),
);
return $blocks;
}
|
namespace Drupal\hello\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\block\Annotation\Block;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a 'Hello' block.
* @Block(
* id = "hello_block",
* admin_label = @Translation("Hello world block title")
* )
*/
|
function hello_block_view($delta = '') {
switch ($delta) {
case 'hello':
$block['subject'] = t('Hello world');
$block['content'] = theme('hello_text', array('text' =>
variable_get('hello_value', 'hello')));
break;
}
return $block;
}
|
class HelloBlock extends BlockBase {
public function build() {
return array('#theme' => 'hello_text', '#text' =>
\Drupal::config('hello.settings')->get('hello_value'));
}
}
|
src/Form/HelloForm.php |
|
function hello_config_form() {
|
namespace Drupal\hello\Form;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class HelloForm extends FormBase {
public function getFormID() {
return 'hello_form';
}
public function buildForm(array $form,
FormStateInterface $form_state) {
|
$form['hello_config'] = array(
'#type' => 'textfield',
'#title' => t('Who are you ?'),
'#size' => 10,
'#description' => t('Text for the hello world block.'),
'#default_value' =>
variable_get('hello_value', 'anonymous'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
|
$form['hello_config'] = array(
'#type' => 'textfield',
'#title' => t('Who are you ?'),
'#size' => 10,
'#description' => t('Text for the hello world block.'),
'#default_value' =>
\Drupal::config('hello.settings')->get('hello_value'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
|
function hello_config_form_submit($form, $form_state) {
variable_set('hello_value',
$form_state['values']['hello_config']);
}
|
public function submitForm(array &$form,
FormStateInterface $form_state) {
$form_values = $form_state->getValues();
\Drupal::service('config.factory')->getEditable('hello.settings')
->set('hello_value', $form_values['hello_config'])
->save();
}
|
drupal7-hello-world/hello-text.tpl.php |
|
Hi from template : <?php print $variables['text']; ?> |
Hi from template : {{ text }}
|