Generating a Tag0:40
For the purposes of this tutorial, we're going to build an extension to keep it simple, but I will show you how to build and generate all that boilerplate code that you need as well. Our extension can be anything. It could be a custom field type, it could be a tag, modifier, it could modify core service provider level stuff, but let's just do something that tends to be more common and really helps enhance your site building capabilities, which would be to make a tag. Let's make a tag that pulls a random quote from a list of quotes. We're going to do php make:tag, and then we'll give it the name of the tag, which is QuoteFetcher. Let's jump into our project, go to QuoteFetcher, and you can see that it lives here in app,
Understanding Tag Methods1:20
which is QuoteFetcher. Let's jump into our project, go to QuoteFetcher, and you can see that it lives here in app, and it's just a class inside the tags directory. Tags are honestly pretty simple to build and understand. The index method, which is basically the basic tag call without a colon and any sort of argument there, and then any additional methods, like an example method, can be mapped to colon example. We'll get rid of this one, and we'll just work on the basic index method. First thing first, we're going to need a list of quotes. Next, we're going to return the quotes.
Returning Random Quotes2:00
First thing first, we're going to need a list of quotes. Next, we're going to return the quotes. Let's go to our Footer class and pull one of these in. We'll jump in here at QuoteFetcher. Actually, we need an underscore there because of the way it's title cased. QuoteFetcher. Let's put value in here because we're looping through a flat list. If we refresh, now you'll see that all of the quotes have been rendered. If we want to return a random one, we can return Quotes::arrayRand, Quotes. You'll see we'll get a fresh one every time.
Adding Tag Parameters2:40
If we want to return a random one, we can return Quotes, ArrayRand, Quotes. You'll see we'll get a fresh one every time. Obviously, there's a lot more you can do with tags. For example, you can accept parameters, and those parameters can do any number of things you decide. For example, if you wanted to have a limit parameter, we could create it, which would be fetched from the params object. Here we can just get the name of the parameter, which is limit, and set a default, which let's set to 1. Since we know it's going to be a number, we can tell it to fetch an int and have the data type strictly evaluated. If we just do a little dump and die here, we can see it's returned 1 because that's the default.
If we just do a little dd here, we can see it's returned 1 because that's the default. But if we pass in limit 5, we can tell that that is accepted. We could set up an author here, say like Ron Swanson. We have a more complex data type. Something like this. Maybe another author. Tom Haverford.
Maybe another author. Tom Haverford. It can have its own list of quotes. Then we can have an author parameter. We'll just get author, set the default to Ron Swanson. We can make this a collection. Next we want to have the filtered quotes. So we'll get quotes where author is author. We'll get all those results.
author is author. We'll get all those results. Filtered quotes and the quotes array. Oh, I need to get the first, not all. You probably noticed this, but I forgot. This needs to be an array here as well. We're back to looping through them all. If we apply what we learned from our antlers video, we could assign quotes to a quote variable. Then pipe on a modifier.
we could assign quotes to a quote variable. Then pipe on a modifier and limit that to 1 right here. That's getting a little bit crazy. I think a more reasonable thing to do would be to leave it alone. In this particular case, just return a random one. Obviously you can apply these kind of building blocks to fetching content from an API to doing any sort of thing to work with custom data or custom markup in a way that you want to manipulate in php instead of in your template.
Packaging as Add-on6:11
to work with custom data or custom markup in a way that you want to manipulate in php instead of in your template. Tags are a really good place to put logic that is too complicated for your templates. If you wanted to do all this as an actual add-on and distribute it, first I would recommend building a better add-on that does something more useful. Here's how you go about doing that. Please make add-on in this case. You're going to give it the full package name. This is going to be like your Packagist package, usually the same thing as your GitHub. In this case, we'll do statamic-quote-fetcher.
This is going to be like your Packagist package, usually the same thing as your GitHub. In this case, we'll do statamic-quote-fetcher. In this case, we'll have it create a tag. This is going to create the Composer package. It's going to update your existing project's Composer and link to the symlink to it and also create all the files that you're going to need for that add-on. You can see up here in this add-ons directory here, we now have our entire package here. We've got a readme that is set up for you to update. We've got our Composer information, which has all the proper settings. We've got a .gitignore file, and this source directory is where your add-on lives.
We've got our Composer information, which has all the proper settings. We've got a .gitignore file, and this source directory is where your add-on lives. We've got a service provider, which is already wired up for you. Because we know you're making a tag, this is what that does. You write a readme, drop your code that you already wrote here into this tag file, and you're basically good to go. Push that up to GitHub, put it on Packagist, and add it to the Statamic Marketplace.
