How to use the extract() function to convert an array to string variables to use in a template

Description

Collection: Drupal 8 Developer PrepChapter: How to Work With Symfony Components

In this video we make our template even more template-like by passing simple string variables in. For the magic of getting values out of our objects and into strings, we use the native PHP function, extract().

Transcript

So another thing we can do in order to simplify our templates is to allow access to variables directly from the template instead of using our request. Let's take a look at hello dot php for an example. So instead of using request get here, wouldn't it be cool if we could just use name as a variable? Well, using a combination of a native PHP function called extract and another method with the request class, we can do that.

Let's take a look. I'll jump back to our Resource Pack and let's open up the nineteenth step which is called front dot php extracting variables to use directly in a template. Let's copy this code and paste it over the code that's currently in the front dot php file and save it.

Now, let's look at the differences between this file and our previous one. So I'll choose the sixteenth step and the nineteenth step and drag these over to my file comparison tool. So our first change is that we have removed the creation of the response object from the top of this file.

Instead of this happening here, we are creating the response inside of this if-then statement where we check to see if the route exists inside of our mapping. The reason for this is that we no longer need this response object inside of our template files which is why we have this above to begin with. Now it's just a little bit cleaner to set up our response objects right here when we return our template or we return a 404.

The next change is that if we assume that all of our pages are going to exist in the same folder, then we are just repeating code here. This DIR source pages and then the dot php at the end of the file name are all going to be the same. So we're duplicating code by adding additional pages that has that code.

So, instead, in our new code, what we're doing is just specifying the name of the file without the dot php extension and then we are building up the entire directory here when we include it. Now, instead of map path being the full directory path to our file, it's instead just this little token here. And so what we're going to do is pass that into an sprintf function that will build up this entire directory and then replace the name of the file here with the value from the key inside of our mapper.

Other than this part here, this ob start, include, and response are nearly identical here except we have one more line that we have added in. You see that this is green. That means that i ...

This collection will not only prepare you to work with and understand Drupal 8 under the hood, it will increase your skills and relevance as a developer. We cover Object Oriented Programming (OOP), how to use Composer, what the Symfony framework is and how to use its various components, plus lots, lots more. For developers really wanting to modernize their skill set, this is a great place to start.

Before watching this series, you will need a web environment with Drupal 8 installed. Follow the videos in the Setting Up a Web Environment With Drupal to set up a web environment. In the series, we use a couple different versions of Drupal 8 and provide instructions for installing each one so you can follow along with the series exactly. You can also use the latest version of Drupal 8, but there will be some slight differences between what you see in the videos and how you would put what you see into practice, but working through those differences can be excellent for learning.

Some of the key points we'll be covering include:

How to build basic module scaffolding in Drupal 8

How Object-Oriented Programming (OOP) works and how to apply what you learn to coding in Drupal 8

How to work with Symfony components included in Drupal 8

How to use Composer to keep track of code dependencies and leverage a huge library of PHP components

Who this collection is for

This collection is mostly for Drupal developers who want to upgrade their techniques to work with Drupal 8. Because many of these techniques are applicable to PHP coding in general, it can also be useful if you simply want to develop your PHP skills and wrap your mind around object-oriented programming.

Prerequisites

You will get the most out of this collection if you are comfortable with procedural coding in PHP. We will make references to how code works in Drupal 7, so it will be helpful if you have some Drupal coding experience in particular.

It looks like there might be an issue playing videos in this browser. We're working on better cross-brower experience, but in the meantime please try the latest Chrome or Firefox browsers. Or, you may be able to watch the video directly without progress tracking or transcript: