CHAPTER 10

image

Adding PHP-Generated Output to Your Drupal Module

Now you can plug your knowledge of PHP into your basic Drupal module. The hello_world.module file itself in your module, of course, uses PHP as the essential piece of the module, the Controller, the class, and more. But also in this chapter you’ll see how to use PHP to help construct the output on the screen for the user. Knowing PHP well will allow you to add dynamic content to your module, which would require variables, if statements, and other complex logic. PHP is used as the nuts and bolts of Drupal itself and all its modules, and it will be a staple for all of your custom module work.

Image Note  The code for this Drupal module is on Github: https://github.com/barnettech/drupal8_book/tree/with_php/hello_world.

Adding Custom PHP to the hello_world.module File

In the new HelloWorldController.php file we use objects, a foreach loop, and some other PHP constructs to add to the module. The HelloWorldController.php file now should look as follows (newly added code is shown in bold):

<?php
     /**
      * @file
      * Contains Drupalhello_worldHelloWorldController.
      */

     namespace Drupalhello_worldController;

     /**
      * Provides route responses for the hello world page example.
      */
     class HelloWorldController {
       /**
        * Returns a simple hello world page.
        *
        * @return array
        *   A very simple renderable array is returned.
        */
       public function myCallbackMethod() {
         $content = '
         <div class="myDiv">
         <div class="bg"></div>
         <div class="block-title">A basic Drupal page created
         programmatically, Hello World</div>';
         $x = 1;
         // Declare a new object called $my_object and per
         // https://drupal.org/node/1817878 you need to reset to
         // the global namespace by prefacing stdClass() with stdClass()
         $my_object = new stdClass();
         $my_object->first_name = 'Jimbo';
         $my_object->last_name = 'Bob';
         $content .= 'Hello ' . $my_object->first_name . '
         ' . $my_object->last_name . '</br>';
         foreach($my_object as $property) {
           $content .= 'property ' . $x . ' in the object is :
           '. $property . '</br>';
           $x = $x + 1;
         }
         $content .= '<br />
             Some random text to show off this transparent background ....
             Lorem ipsum dolor sit amet, consectetur adipiscing elit.
         Morbi nisi purus, gravida sit amet molestie et, facilisis vel
         nulla. Mauris faucibus augue eu quam feugiat at lacinia velit
         malesuada. Sed bibendum faucibus mattis. Maecenas quis ligula
         nibh, sit amet iaculis metus. Aenean lobortis massa ut nulla
         tristique eu vestibulum leo eleifend. Maecenas arcu lectus,
         facilisis in mattis sed, pretium et metus. Phasellus elementum,
         elit fringilla mollis sollicitudin, ipsum odio vestibulum quam,
         vitae tristique odio tortor eu augue. Pellentesque volutpat
         placerat neque, sit amet vehicula lectus commodo vitae. Aliquam
         nec mauris pharetra velit tincidunt consectetur. Aliquam vitae
         lectus nisi. Curabitur mi sapien, ultrices quis bibendum eu,
         mauris pharetra velit tincidunt consectetur. Aliquam vitae
         lectus nisi. Curabitur mi sapien, ultrices quis bibendum eu,
         ultricies eget libero. Donec mollis malesuada est a varius.
         Vestibulum dignissim venenatis nisl, nec semper massa tincidunt
         egestas. Maecenas a erat sem.

         montes, nascetur ridiculus mus. Phasellus sed neque ante,
         venenatis sagittis dui. Cras lorem ipsum, scelerisque tempor
         aliquet quis, imperdiet in augue. Curabitur tellus est, ultrices
         eu sagittis et, pellentesque id enim. Nunc lobortis mattis
         viverra. Sed non purus ipsum. Aenean ac justo sed urna eleifend
         consequat.
         </div>';
         $element = array(
           '#markup' => 'Saying Hello World in Drupal 8 is cool!' . $content,
           '#attached' => array(
             'library' => array(
                'hello_world/hello-world',
             ),
           ),
         );
         return $element;
       }
     }

Figure 10-1 shows the resulting web page.

9781430264668_Fig10-01.jpg

Figure 10-1. The updated page that results from the custom Drupal module created using PHP code

Summary

In this chapter you looked at how to use PHP in your custom module. PHP is the nuts and bolts of Drupal itself, its modules, and all custom modules. It is the programming logic that is the brains of the Drupal content management system. You also saw how you could enhance your own custom-created hello_world module by adding in some PHP to help generate output to the screen for the user. In the next chapter you’ll learn how to create blocks programmatically and take an introductory look at the back-end MySQL database system, which is most often used as the back-end data store for Drupal.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.35.229