18.11.2015 Views

Beginning Drupal 8

Todd Tomlinson - Beginning Drupal 8 (The Expert's Voice in Drupal) - 2015

Todd Tomlinson - Beginning Drupal 8 (The Expert's Voice in Drupal) - 2015

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12 ■ Anatomy of a Module<br />

The following line is the last half of the call from our router (HelloController:sayhello). The line of<br />

code defines the function that will return the information back to the page that is displayed when the visitor<br />

accesses the /hello URL on our site.<br />

public function sayhello() {<br />

The remaining code simply calls the function we created in our module file hello_hello_world(). This<br />

function returns the text “Hello <strong>Drupal</strong> 8 World!” in a renderable array that <strong>Drupal</strong> knows how to display on<br />

a page, and returns it back to our modules router.<br />

return array(<br />

'#markup' => hello_hello_world(),<br />

);<br />

Save this file, and we’re ready to enable our new module! Visit the Extend page in the admin section of<br />

the site and scroll down until you see the Awesome Modules section (see Figure 12-2).<br />

Figure 12-2. Our Hello <strong>Drupal</strong> 8 World! module on the Extend page<br />

Check the box next to the module’s name and click the “Save configuration” button to enable the module.<br />

With the module enabled, you’re now ready to test your first <strong>Drupal</strong> 8 module! To execute the module, navigate<br />

to your homepage and add /hello to the end of the URL (we defined that path in our module’s routing file).<br />

You should see the output shown earlier in Figure 12-1.<br />

Other Module Files<br />

Our Hello module is a very simple module, the purpose being to help you past the initial learning curve of<br />

how a module is structured in <strong>Drupal</strong>. Now that you know the very basics, you can look at other contributed<br />

modules, or even <strong>Drupal</strong> core, to see how more complex modules are created and study the files associated<br />

with that increased complexity. You can learn about writing more complex modules at www.drupal.org/<br />

developing/modules/8. I highly recommend downloading the Examples module from www.drupal.org/<br />

project/examples. Within that module you will find several examples that demonstrate how <strong>Drupal</strong> 8<br />

modules are constructed, a great starting place for creating <strong>Drupal</strong> 8 modules.<br />

Summary<br />

Congratulations! You have written a <strong>Drupal</strong> 8 module and earned your first stripe toward becoming a <strong>Drupal</strong><br />

module developer ninja. The purpose of this chapter was to give you an overview so you would have enough<br />

information under your belt to go exploring through other modules. While the learning curve can be steep<br />

for <strong>Drupal</strong> module development, you’ve taken the first step along that path<br />

145

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!