Skip to main content
9 votes

Get the type of action and the elements(s) that an action is being performed on when listening to elements.onPerformAction?

It appears that when one or more Entries (or other types of content) are manipulated (in my case, deleted) from the bulk edit view in the Control Panel, the type of events that are triggered are ...
jthomas's user avatar
  • 373
9 votes
Accepted

Infinite getPrev and getNext?

You would need query for these entries using the first and the last method. {% set criteria = craft.entries.section('blog').order('title asc') %} {% set prevEntry = entry.getPrev(criteria) ?: ...
carlcs's user avatar
  • 36.3k
5 votes

Ability to SUM() or GROUP BY from the template or ElementCriteriaModel

In Craft 3 you can do this without a plugin: {% set hours = craft.entries .section('channelTimetracking') .sum('field_myCustomField') ?? null %} Details in the docs.
Ottó Radics's user avatar
5 votes
Accepted

A more economical method of building an archive menu?

If you know which years should appear, and there are no breaks in the timeline, perhaps it would make more sense to do a simple numeric loop instead... {% for year in (now.year)..2009 %} {{ year }...
Lindsey D's user avatar
  • 24k
5 votes
Accepted

Performance when loading lots of Elements but only need limited data

Not in Craft 2 without completely bypassing ElementCriteriaModel and building your own SQL query. In Craft 3 (currently in beta) you can, though – you can override which columns get selected in your ...
Brandon Kelly's user avatar
5 votes
Accepted

Pagination and Entry Model on Homepage

project.images is an array because you are eager-loading the images. Since you’re only loading one project entry, you’re not actually gaining anything by eager-loading the images (or category), ...
Brandon Kelly's user avatar
5 votes
Accepted

.first() vs. .one() in craft 3

.all() returns a collection of elements, which you can loop through with a for loop. .one() returns a single element, so you cannot use a for loop to access it. In this case you should just store ...
Jeremy Gimbel's user avatar
4 votes
Accepted

Query all categories once and get individual categories from array

Your issue is that as soon as you call .find(), your ElementCriteriaModel returns an array of elements. In other words, the ElementCriteriaModel is gone, and it's no longer possible to add parameters (...
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

How can I query for entries that have one of two Lightswitch fields enabled?

Craft 3 Thanks to the where() function this has become easy in Craft 3. {% set query = craft.entries.where([ 'or', {'field_lightswitchA': 1}, {'field_lightswitchB': 1}, ]) %} Craft 2 ...
carlcs's user avatar
  • 36.3k
4 votes
Accepted

Craft Commerce update variant field in plugin

This is the general mechanism for updating custom fields on elements - use setContentFromPost and then call the element's save function. $variant->setContentFromPost(array( '...
Jeremy Daalder's user avatar
4 votes
Accepted

Filter entries in the CP based on custom permission

After some digging I came up with some answers myself. For future reference, here they are: Is the event triggered by the CP: if(craft()->request->isCpRequest()){} Check the logged in user's ...
Tom De Smet's user avatar
  • 1,456
4 votes
Accepted

Figuring out why my image is not working in the template

You can have more than one Asset in any Assets field, so it always returns to you an array of assets. So you just need: {{ entry.hoofdafbeelding.first().getUrl('square') }} ...assuming you want the ...
andrew.welch's user avatar
  • 11.6k
4 votes
Accepted

How do I query two relationship fields where one relationship MUST be empty?

Splitting the criteria up like this works. $criteria->relatedTo = [ 'targetElement' => anElementId, 'field' => 'relationshipOne' ]; $criteria->relationshipTwo = ':empty:'; ...
Benjamin Kohl's user avatar
3 votes
Accepted

Complex Conditionals with ElementCriteriaModel / Element API

After doing a bit more digging, I think the only real option here is run a preliminary query to pull out the ids of the elements I want and then pass that into the ElementCriteriaModel. It's not ideal,...
Michael Witwicki's user avatar
3 votes
Accepted

In the Element API plugin, how can I get only tags that are related to a particular section?

The relatedTo parameter accepts either a single element ID or model, or an array of element IDs or models. It doesn't accept asset source handles, section handles, or a particular element type ...
Mats Mikkel Rummelhoff's user avatar
3 votes
Accepted

Define elements that bust cache on save

Hard to know for sure without seeing some plugin code, but my guess is you'll want to use the TemplateCacheService's includeElementInTemplateCaches similar to how BaseTemplate's ...
Brad Bell's user avatar
  • 67.5k
3 votes

Search for entries where a field value is between multiple ranges of numbers

I'm not sure if you can do this in the same query but you can use two queries, one for each range, and then merge them, something like this: {% set range1 = craft.entries({myFieldHandle: ['and', '>...
Mato's user avatar
  • 229
3 votes
Accepted

How to create an element query in PHP with custom field names in variables?

You can use the Craft::configure() for that.. imagine your array looks like $criteria = [ 'age' => ['and', '>=30', '<=50'], 'enabled' => false 'status' => null, ] You can ...
Robin Schambach's user avatar
2 votes

How do I order by _entry type_ when looping through entries in a template?

You could use Craft's group filter for this, but you'll lose the ability to use the paginate tag to paginate your entries by doing so. {% set entries = craft.entries.section('mySection').order('title'...
carlcs's user avatar
  • 36.3k
2 votes

Can I eager load Super Table fields in a Matrix block?

This appears to have been added in the 0.4.7 release! View the original issue here. Note: I have not personally verified this functionality.
August Miller's user avatar
2 votes
Accepted

How to create a query to group alternate language pages?

This query cannot be created in the "ideal" way described in the question using craft()->elements->getCriteria. It required several steps to build the results needed. The final solution was a ...
Ben Parizek's user avatar
  • 13.5k
2 votes
Accepted

Sprout Forms -- How to link to individual entries on the front-end

A note on Elements Craft allows plugins to create custom Element Types. Elements have the option to support URIs, but it is not required. Out of the box, Craft currently has 7 Element Types and only ...
Ben Parizek's user avatar
  • 13.5k
2 votes
Accepted

Element API "Last Updated Sections"

This isn't really a job for the Element API plugin. You can accomplish what you want much easier by just creating a plain ol' Twig template (e.g. templates/updates.json) that pulls the three latest ...
Mats Mikkel Rummelhoff's user avatar
2 votes

"Natural" sorting for ElementCriteria

You can fake it by first sorting by character length, and then by the value: $criteria->order = 'CHAR_LENGTH(myField) asc, myField asc Its not perfect, but in certain cases, it will be all you ...
Tim Kelty's user avatar
  • 3,081
2 votes
Accepted

Return element criteria model from attribute of a model

If I'm following you correctly, seems like you could override your Model's getAttribute method to do something like this completely untested code: public function getAttribute($name, $flattenValue = ...
Brad Bell's user avatar
  • 67.5k
2 votes
Accepted

Is it possible to use the MySQL LIKE directive in an ElementCriteriaModel?

The * wildcard character should do the trick. c.f. https://craftcms.com/docs/templating/elementcriteriamodel#parameter-value-syntax $assets = craft()->elements->getCriteria(ElementType::Asset, ...
Michael Rog's user avatar
  • 3,420
2 votes

Limit ElementAPI results with custom date/time field

After some exploration and trial and error, I was able to find a solution. Ultimately there are two problems: startDateTime, endDateTime and php’s date() function were not returning the same timezone ...
Taylor's user avatar
  • 123
2 votes
Accepted

ElementCriteriaModel UTF-8 Encoding

Could this maybe be a unicode issue? Maybe the api plugin is producing \u2019 instead of the unicode to produce a normal '
jooosh's user avatar
  • 190
2 votes
Accepted

Caching ElementCriteria Lookups In Craft CMS Plugins

I have figured it out, but am not sure if this is the best way so could still use some advice. # Get a unique hash of your search criteria (still figuring this out). $hash = md5(json_encode($...
JamesNZ's user avatar
  • 947
2 votes
Accepted

how can I filter to get only root categories in elementapi.php

To get all root categories you can use: 'criteria' => ['level' => 1]
Sean Tilley's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible