Brandon Kelly
  • Member for 7 years, 7 months
  • Last seen more than a week ago
Test for empty field?
Accepted answer
51 votes

The answer depends on the field type. Assets, Categories, Checkboxes, Dropdown, Entries, Matrix, Multi-select, Radio Buttons, Tags, and Users fields each will each appear to be non-empty, even if ...

View answer
413 “Request Entity Too Large” error with uploading a file
Accepted answer
47 votes

413 errors occur when the request body is larger than the server is configured to allow. Here’s how you can fix it, depending on your web server: Apache: Set the LimitRequestBody directive in either ...

View answer
What’s the recommended way to set the site URL?
Accepted answer
47 votes

As of Craft 2, you should be using the new siteUrl config setting. There are two advantages to setting your site URL using this config setting. First, The value can be set on a per-locale basis, in ...

View answer
What are the best practices for using the {% cache %} tag?
Accepted answer
39 votes

Generally you’ll just want to cache areas of your templates that are going to trigger a lot of database queries, or fetch a lot of data, since those are going to have the biggest hit on your site’s ...

View answer
How can I automatically generate responsive images from a single hi-res asset?
Accepted answer
38 votes

You can do this using image transforms. There are a couple things you’re going to need to consider though: Not every image will necessarily be high-res (e.g. a computer screenshot taken on a non-...

View answer
What are the most common template performance gotchas to avoid?
Accepted answer
28 votes

Avoiding unnecessary DB queries is definitely the main thing to watch out for. Here’s an example that came up recently in support: Someone wanted to display the first 6 Matrix blocks in an entry, ...

View answer
How can I retrieve channel data in json format?
Accepted answer
27 votes

Twig is great for outputting HTML/XML markup, but it’s not exactly the best option when you just want to output some data as JSON. The Element API plugin aims to make outputting entries (and other ...

View answer
Is there a way to output only the first paragraph of a Rich Text field?
Accepted answer
27 votes

You can do this entirely with Twig. First, use the |split filter to split your HTML into an array of paragraphs, on their closing </p> tags: {% set paragraphs = entry.myRichTextField|split('&...

View answer
How can I return data from a Matrix field in random order?
Accepted answer
27 votes

You can either have MySQL select the blocks in a random order using RAND(): {% set blocks = entry.myMatrixField.order('RAND()') %} Or you can shuffle the blocks from Twig instead, using Craft’s ...

View answer
How do you list categories in a category group?
Accepted answer
27 votes

You can fetch your categories on the front end using craft.categories, which has a group param that can be used to filter categories by a given group handle. {% set categories = craft.categories....

View answer
How do I target the first item in a Twig for loop?
Accepted answer
24 votes

If the “loop” is an ElementCriteriaModel object, then you can grab the first element using .first(): {% set blogPost = craft.entries.section('blog').order('posdDate desc').first() %} -- {% set image =...

View answer
Image transforms returning cpresources url
Accepted answer
21 votes

That’s what getUrl() returns if the transform hasn’t been generated yet. When the browser requests that URL, Craft will generate the transform, and respond with a 302 redirect to the actual image URL. ...

View answer
Get the current section handle
Accepted answer
21 votes

One thing that’s important to remember is that there’s no inherent relationship between your templates and sections. Any template can show any entry from any section. So there’s no such thing as the “...

View answer
How do I output the locale’s name?
Accepted answer
21 votes

You can get the current locale’s name via craft.i18n.getCurrentLocale(). {% set locale = craft.i18n.getCurrentLocale() %} {{ locale.name }} That will output the name using the current locale’s ...

View answer
Transform Info from Handle?
Accepted answer
21 votes

Sortof – you can find out what an image’s width would be with a transform applied to it via AssetFileModel’s - getWidth() and getHeigth() <img src=" {%- if image.getWidth(transform) > 970 -%...

View answer
What's the best way to set up a content-manageable 404 page?
Accepted answer
20 votes

I would create a Single with the URI set to "404", and also set its Template to "404". That entry will automatically get loaded if you go to http://example.com/404, and load your 404.html template, so ...

View answer
Solution for Matrix-within-Matrix (or More Robust Table Fields)
20 votes

Rather than trying to store multiple stats together in a single block, another option would be to make a "Stat" (singular) block type, which only holds a single stat, and just have authors create as ...

View answer
How can my plugin generate a generic modal window in the Control Panel?
Accepted answer
19 votes

Create a div with whatever contents you want in the modal, and with a class "modal". Then create a new Garnish.Modal object with it: var $div = $('<div class="modal">...</div>'); var ...

View answer
How to save Matrix data programmatically in Craft 3
Accepted answer
19 votes

Saving Matrix fields programmatically got a lot easier in Craft 3 thanks to all the content refactoring. use craft\elements\Entry; use craft\elements\MatrixBlock; // Figure out the section & ...

View answer
How do I add a subcategory to the URL slug for a channel entry?
Accepted answer
18 votes

If you go into your Category Group’s settings and set the following settings: Craft 2 Top Level URL Format: {slug} Nested URL Format: {parent.uri}/{slug} Craft 3 Category URI Format: {parent.uri}/{...

View answer
What is getContent() / the ContentModel, and how do I use it to get and set custom field values?
Accepted answer
18 votes

BaseElementModel::getContent() will return the element’s ContentModel. It has two purposes: To store the element’s data from the craft_content table table when fetching elements from the DB. To store ...

View answer
How can I incorporate a variable into a string?
Accepted answer
18 votes

If you‘ve got a multi-lingual site (or even if you don’t), you can pass variables into strings with the |t filter: {{ "Welcome back, {name}"|t( {'name': currentUser.name} ) }} (See http://...

View answer
Include a template that sets variables (global template variables)
Accepted answer
17 votes

There are actually two issues with this approach. The first, and the direct reason you’re getting that Twig error, is that templates that extend another template can only output things within {% block ...

View answer
How do I set mime-types for Craft templates?
Accepted answer
17 votes

If you want to explicitly set the mime type from your template, you can do it with Craft's {% header %} tag: {% header "Content-Type: application/json" %} If it is already set via this method, Craft ...

View answer
How do I import data into Craft?
15 votes

If you’re comfortable with PHP, Craft has some plugin APIs that make creating content relatively painless. For example, EntriesService::saveEntry().

View answer
Move entries between sections
Accepted answer
15 votes

It’s currently only possible via direct manipulation of the entries in your database. You will first need to visit your sections table to determine the desired section ID, and then the entrytypes ...

View answer
Is it possible to use the ElementType Modal UI for a custom Field Type Plugin?
Accepted answer
14 votes

Yep, you can do that with Craft.createElementSelectorModal(): var myModal = Craft.createElementSelectorModal('Entry', { resizable: true, storageKey: 'someUniqueID', ...

View answer
How can I make a template output its own filename?
Accepted answer
14 votes

You can get the current template’s name like so: {{ _self.getTemplateName() }}

View answer
How can I order entries by title when each title is numeric?
Accepted answer
14 votes

You can do that using MySQL’s cast() function: craft.entries.section('mySection').order('cast(title as unsigned)') Alternatively, you might get better performance if you create an actual Number ...

View answer
Live Preview Button missing
Accepted answer
13 votes

For the Live Preview button to show up, two criteria must be met: Your section’s “Entries in this section have their own URLs” setting must be checked Your section’s “Entry Template” setting must be ...

View answer
1
2 3 4 5
18