Tip:
Highlight text to annotate it
X
Okay. So with our two files and our info dot yml file filled in, we should be able to see
our module show up on the module listings page.
So jump back to the browser and we are going to expand the Menu by clicking the Menu
item at the top, and then we'll move the Menu off to the side just to give us a better perspective
on the page that we're viewing. So we're clicking the side menu icon here which moves it over
here, and let's click on Extend.
In Drupal 7, this page would've been called Modules. It's called Extend now which is a
little bit more descriptive of what you want to do when you go to the page.
Now, there is also a Search up here and so instead of scrolling down, we could actually
type in glue and see our Build A Module group and our Glue module here. And if we check
that, we can click Save configuration and get it installed.
All right. So we were able to install it without a problem. Now, our module doesn't do anything
yet so let's change that.
Let's go back to our module folder and let's open up our glue dot module file which will
be empty, and we'll go back and in our steps folder, go ahead and open up the third step
which is called glue dot module hook page alter. Copy the entire code of this page and
paste it over into the glue dot module file and save it.
Now, this file doesn't do much. What it does is implement a hook called hook page alter.
This takes the page render array and allows us to modify it in some way.
Now, the only thing I did to prepare this example was to use a debugger to explore what
the page structure was, and then within that I simply added a markup item which will allow
you to plug in some HTML, and I'm putting in hello content.
Now, just because we've made this change doesn't mean that it will actually apply in our module
just yet because there's a cache that keeps track of which modules implement which hooks
and we need to rebuild that by clearing our caches.
So we can do that by going back to the browser, and I'm going to open up Configuration in
another tab here because clearing the cache is just something that we'll do fairly regularly
through this video series. And then on the Configuration page, we're going to click on
Performance.
And here at the top, we have Clear all caches so we'll click that. And then if we go back
to our page and exit out of the overlay which will refresh the page, we'll see our hello
content here above the content on this homepage. And in fact, any page we go to on our Drupal
site that's using this theme is going to have this hello content at the top.
Now, the goal of this example so far is simply to show you that we are able to build a module
that impacts the site in some way without doing anything too crazy. We haven't even
touched on object-oriented programming or any of the additional systems that Drupal
8 introduces and yet we are still able to make a significant impact on a page. We can
actually override the whole output of the page if we wanted to. We instead opted to
add something to it.
So one of the most common hooks to use in Drupal is hook form alter which allows you
to alter the output of a form in some way. So let's take a look at adding a hook form
alter to our module as well.
So let's go back to our file browser and open up the fourth step which is called glue dot
module hook form alter. And just like before, we are going to copy the entire code and jump
over to our glue dot module file, and we are going to paste over the code that's already
there and save it. So you see, we have our glue page alter here where we're adding some
markup to the content of the page, and now we have our hook form alter function as well.
Now, if you ever did a hook form alter in Drupal 7, this will look nearly identical.
So just like in our other hook, we are simply adding a little bit of extra HTML to our form.
So first, we are checking to see if the form id is search block form which, if we jump back
to the browser, is this form right here. I'm going to go back to the editor, and we're
just adding an element to the form called fun text and all it is is a little bit of
markup that says Search our pages like a boss. And it sets the weight to a negative number
which should set it above the other elements in the form.
So if we go back to the browser and refresh, we're not going to see our content in the
form because we need to clear our caches again in order to register this alter hook with
the cache.
So we'll go back to the Performance page and click Clear all caches, and let's jump
back to our page and refresh. And now you see our content in the search form here.
Okay. So we are not going to go into great detail about the way these hooks that we just
implemented work. There are some videos in the Drupal 7 development series that cover
this, and soon there will be some videos that cover it in Drupal 8. So if you're unfamiliar
with Hooks, go ahead and take a look at those videos if you want to dive in a little bit.
But the main point is that we can still get a lot done without having to deal with classes
or object-oriented programming. From here though, we are going to step into that object-oriented
programming world, and the first example we are going to use is adding a page programmatically
through our module.