Mike Pepper
  • Member for 7 years, 7 months
  • Last seen more than a month ago
Section entries - order by random
Accepted answer
46 votes

You can use MySQL's RAND() function inside your order method: {% for entry in craft.entries.section('team').type('mainPeople').order('RAND()') %}

View answer
Is Craft Suitable for Large Public Body website
Accepted answer
18 votes

One of the biggest Craft projects I've worked on has around 25 sections, 10+ global sets, 250+ fields, and 12,000+ elements across 16 locales (although only 1 locale has launched atm). Craft itself (...

View answer
How to get absolute path to asset file?
Accepted answer
15 votes

You can get the rest of the path from your asset's source and folder, I think something like this should do it: $source = $asset->getSource(); if ($source->type != 'Local') { throw new ...

View answer
Pass variable into include
Accepted answer
14 votes

You can leave the with lightswitchTest part off altogether, and all the variables from the parent template will be available inside the include too. with expects an array, where the keys correspond ...

View answer
Triggering the start of a task from a CRON job
Accepted answer
10 votes

Everything you need to manage tasks is in the TasksService, so you should be able to just use that in your controller action to kick off any pending tasks, using something along the lines of: if (!...

View answer
Uploading PSD files not possible
Accepted answer
9 votes

You can add the psd extension to extraAllowedFileExtensions in your config file. 'extraAllowedFileExtensions' => 'psd' https://craftcms.com/docs/config-settings#extraAllowedFileExtensions

View answer
Conditional Parsing Issues
9 votes

This looks like a situation where you might get away with just using Twig's default filter: {{ entry.metaTitle|default(title|default(siteName)) }} It works even with undefined or empty strings, and ...

View answer
Linking to a plugin setting page?
Accepted answer
9 votes

Add a public getSettingsUrl() function to your main plugin file that returns a URL string. A full URL is not required -- you can simply return "pluginname/settings". If this is left blank, ...

View answer
Is it possible to use _includes/forms outside of the Control Panel?
9 votes

I've been able to render CP templates in the front end using some methods in PathService and TemplatesService, which you could use inside your own Template Variable to render the macros: class ...

View answer
When to use "if" or "without"?
Accepted answer
8 votes

For your example, you should exclude the fictional towns in the initial query itself rather than filtering them out afterwards: {% set towns = craft.entries.section('towns').townFictional('not 1')....

View answer
Can I add an enabled-by-default lightswitch field to existing entries?
8 votes

If there was no default set beforehand, then your previous elements will have a value of null for that field. You should be able to include those in your "on" queries by querying for that null value ...

View answer
When using getCriteria() along with buildElementsQuery() raw html gets displayed by the template
Accepted answer
7 votes

$query->queryAll() returns raw arrays of data from the database, this is why you're needing to prepend field_ to each attribute, to get the column name. You need to populate some EntryModels ...

View answer
Trouble with curly braces in Entry URL Format / renderObjectTemplate
Accepted answer
6 votes

It's not documented so I guess not officially supported, but you can use template variables in the URL formats. For example if you had a UrlFormats plugin, you could use something like this: <?php ...

View answer
Craft Commerce: Detecting & Styling Flash Messages?
6 votes

You can use hasFlash() instead, or pass false as a third argument, otherwise the flash message is discarded after the first time it is accessed. {% if craft.session.getFlash('notice', null, false) %} ...

View answer
How to create code blocks with syntax highlighting
Accepted answer
6 votes

There is the Ace Freely plugin, which adds an Ace Editor Field Type that would work well in a Matrix field.

View answer
How does craft deal with Plugin databases when updating plugin versions?
Accepted answer
6 votes

Every time Craft notices that a plugin's version number has changed (I imagine quite near the start of every request,) it checks for any new Database Migrations in that plugin that might need running ...

View answer
"Eager loading" of relations like Yii's "with" function
6 votes

I've been thinking about ways to achieve this recently, and I've put together a basic proof-of-concept plugin for fetching one level of relations for a collection of elements, with no more than two ...

View answer
Output array to Plugin::log
Accepted answer
6 votes

Passing true as the second parameter to print_r returns a string that can be written to the log. Plugin::log(print_r(array('test'), true));

View answer
Output structure in a <ul>
Accepted answer
6 votes

Craft's built-in {% nav %} tag would save you having to write your own recursive macro: Adapted from http://buildwithcraft.com/docs/templating/nav <div class="panel"> <h2>People</...

View answer
Customizing Craft Commerce Template Paths
Accepted answer
5 votes

You'll need to keep any action URLs as commerce/, as these point to controller actions inside the Commerce plugin, not templates in your store directory. So if you find: <input type="hidden" name="...

View answer
Can I add another filter option to element index views?
Accepted answer
5 votes

I made a small plugin that nests Entry Types under their section in the sources list that might be good enough for what you're after: https://gist.github.com/mdcpepper/548fd04939e040507960

View answer
Saving Category through API
Accepted answer
5 votes

You can get any validations errors on the category after you've attempted to save it with CategoryModel::getAllErrors() or \CModel::getErrors(). You're also setting the groupId on the Category's ...

View answer
How to limit number of entries returned from a group filter?
5 votes

The reason limit() isn't working is that is a method on the ElementCriteriaModel you get from craft.entries. Because you're actually dealing with a standard array when you access catEntries, what ...

View answer
Constructor in custom controller?
Accepted answer
5 votes

I'm not a huge fan of it but for this situation it might be more appropriate to use init(), Looking at Craft\BaseController: /** * Initializes the controller. This method is called by the Craft ...

View answer
How do I output a matrix block type only once when looping through entries?
Accepted answer
5 votes

You can output the first matrix block of it's type using .type(blockHandle): {% for entry in craft.entries.section('mySection') %} {% set photoBlock = entry.tactics.type('photo').first %} {% ...

View answer
Outputting Matrix Loop in Template
Accepted answer
5 votes

It looks like you're pretty close, but those field names need to be called as attributes on block: {% for block in entry.threeHomepageImages.type('images') %} {% if block.image|length %} {...

View answer
Any reason why {% requirePermission 'editEntries:1' %} would stop working within the past week or so?
4 votes

Assuming you've recently updated to Craft 3.1, if you look in your userpermissions table you'll see that they're now lowercase and using UIDs rather than integer IDs, like this: editentries:3bb69b06-...

View answer
Best way to find a deprecation error with no location hint?
Accepted answer
4 votes

If you look in the list of file paths in the stack trace there will be one or more files in the storage/runtime/compiled_templates directory. In this particular case there is storage/runtime/...

View answer
Override admin template with own template
4 votes

For that path, Craft would look for the following template files, and use the first one that exists: craft/app/templates/users/_edit craft/app/templates/users/_edit.html craft/app/templates/users/...

View answer
Getting only slug in otherwise null object on routed template
Accepted answer
4 votes

You will need to use {% set practice = craft.entries.slug(slug).first() %} to fetch the real EntryModel, and not just an ElementCriteriaModel. I think the relatedTo criteria still works without it ...

View answer