Hot answers tagged

12 votes
Accepted

Overwrite value in object in array

Solution This won't work in Twig as presented in the question (see why below). Instead, users would need to be changed to an associative array (or "hash" in Twig terminology) with keys that aren't ...
user avatar
  • 1,887
9 votes
Accepted

Using Twig to merge an array and remove duplicates

You could do it in the twig template But a template isn't really the best place for something that is this codey, it gets pretty unreadable pretty quick. Notes: I start by building arrays of models ...
user avatar
7 votes
Accepted

Adding entries to an array, one at a time

The merge filter only works with 2 arrays (or 2 hashes), not with an array and an object. I think that the event you are trying to add is not an array, it is a single event (Entry Model). You might ...
user avatar
6 votes
Accepted

Shouldn't this merge be working?

Try adding find() to your ElementCriteriaModel, I think without that function you're not actually saving your entries as an array in your variable todaysOneOffEvents yet. Here's what I found in the ...
user avatar
  • 35.8k
6 votes

Using Twig to merge an array and remove duplicates

The accepted answer here should hopefully help you out. Basically: {% set newArray = [] %} {% for name in array %} {% if name not in newArray %} My name is {{name}} {% set newArray = ...
user avatar
4 votes

Merge a multidimensional array

Ok, I managed to figure it out following directions on this page: Creating dynamic array and set keys {% set filmData = { "film" : film.title, "url" : film.url, "starts" : starts, "ends" : ends, "...
user avatar
  • 111
4 votes

Overwrite value in object in array

This could also be accomplished with the map filter, if you’re running Craft 3.1.29 or later: {% set users = [ {name: 'Bill', age: 25}, {name: 'Tim', age: 30}, {name: 'John', age: 32} ] %} ...
user avatar
4 votes

Merging 2 Entries Array's

Just a few side notes: neither the first entries variable nor the second entries variable is an array - both are objects of kind craft\elements\db\EntryQuery (or craft\ElementCriteriaModel if it's ...
user avatar
4 votes
Accepted

How to duplicate entries and loop them without getting a Twig or deprecation error?

Instead of duplicating the entries, you could run your entries loop twice (or as many times as you want) maybe? {% for i in 0..1 %} {% for entry in entries %} {{ entry.title }} <br&...
user avatar
  • 7,095
4 votes
Accepted

Modified A-Z grouping

When looping over the letters in the range, you should be able to use the keys filter to check if the current letter is in the entriesByGroup array. If it is, you can link it or add a class, etc. {% ...
user avatar
3 votes
Accepted

Merging Multiple Relations as params

Something like this should work: {% set myStation = getCookie('myStation') %} {% set relatedStation = craft.entries.id(myStation) %} {% set params = { section: 'blog', limit: 6, offset: 1, relatedTo:...
user avatar
  • 66.4k
3 votes

Merge two queries of entries ordered by publication date

You can probably use the relatedTo parameter with multiple relation criteria: {% set entries = craft.entries.section(['news', 'press']).relatedTo('or', { targetElement: newsTypeCategory, field: '...
user avatar
3 votes
Accepted

Possible to merge and sum the values of an array?

What you need to do is to add the row.points to any existing points for each type, before merging – otherwise you'll just overwrite the existing type/attribute and its values (in other words, the |...
user avatar
3 votes
Accepted

Query two sections filtered by different custom fields, then order them

There's this super useful SuperSort plugin that would make this possible. {% set sortAsTemplate = '{{ object.section == "Exhibitions" ? object.startDate|date("U") : object.date|date("U") }}' %} {% ...
user avatar
  • 35.8k
3 votes
Accepted

If merging two sections from different locales how do I then sort them by a shared criteria?

With that last {% set news = craft.entries.id(ids) %} criteria model you're querying in one locale only and there's no way to change that, Craft doesn't have a feature to set up a criteria model to ...
user avatar
  • 35.8k
3 votes

Create an object of objects with merge

You're overriding the "title" and "link" properties on every iteration. Wrap the itemObject object in [] to merge the whole object into your array. {% set object = object|merge([itemObject]) %}
user avatar
  • 35.8k
3 votes

Merging Hash Error

You are setting option to a string here: {% set option %}...{% endset %} And merge takes an array or a hash as a parameter. It looks to me like you want option to be a hash: {% set option = {factor:...
user avatar
3 votes
Accepted

Correct way to merge craft.user field arrays and use outside the loop?

It looks like you have a typo in your merge, and the structure of the merge is incorrect. {% set merged = specialisms|merge(specialisms) %} This is saying set merged to the array specialisms merged ...
user avatar
  • 608
3 votes

Merging classes in Twig based on component and context

You can solve this using the merge filter. When merging hashes (i.e. associative arrays), properties from the merged hash override properties with the same key in the target hash. Since you're ...
user avatar
  • 6,030
2 votes

How to merge/combine/transfer-entries-between two categories in the CP?

This could easily be accomplished with a MySQL query on your DB. Of course, BACK UP YOUR DATABASE FIRST! Example, assuming the old category ID is 1 and the new one is 2... UPDATE `craft_relations` ...
user avatar
2 votes
Accepted

filter matrix blocks by multiple light switch value, and then merge them

Keep in mind objects in PHP are always called by reference not by value thus your anchors query will always return blocks from the type subhead with the field value anchor =1 while your matrix query ...
user avatar
2 votes

Using Merge filter

What your merge filter does now, is merge the new object with the previous one, thus overriding the values for both keys. Try this: {% set allWorkshops = allWorkshops|merge([{ 'eventDate' : ...
user avatar
  • 6,288
2 votes
Accepted

How to merge SEOmatic object in Craft 3?

Based on the SEOmatic docs, something like this should do the trick nicely: {% do seomatic.meta.seoTitle("my title") %}
user avatar
2 votes
Accepted

Merging params PostDate limits errors

try: {% set params = params | merge({ postDate:['and', '>= ' ~ start.date, '< ' ~ end.date] }) %}
user avatar
  • 7,095
2 votes

Paginating Merged Element Queries

I think you're correct in how you currently do it. You can't merge Element Queries, you can merge IDs (or any array of anything, really). The only thing you should remove, is the all when settings ...
user avatar
  • 6,288
2 votes

Paginating Merged Element Queries

Here's how I'd do it currently: {% set cat1 = craft.categories.group("catAttributes").slug('diabetic').one %} {% set firstEntries = craft.entries.section("cats").relatedTo([cat1]).ids() %} {% set ...
user avatar
2 votes
Accepted

Pagination and "if" condition

You can build an array of the filtered results, then paginate that. {% set entries = craft.entries().section('produkte').relatedTo(category) %} {% set filteredEntries = [] %} {% set existingtitle = []...
user avatar
  • 2,437
2 votes

Problem with data hash only storing last entry in loop

For the hash key to be dynamic, you must wrap it in parentheses: (i): {
user avatar
2 votes

Merge, shuffle, and paginate 😱

Got some help and finally got this working. Here's the final code for my future self and anyone else that may need this: {% set thisService = entry.slug %} {% set shufflePremium = shuffle(craft....
user avatar
  • 299
2 votes

Merging classes in Twig based on component and context

(Whilst this answer does not directly answer your question (instead see the answer by MortizLost), I wanted to add it to show a different approach to the problem you're trying to solve). Tailwind CSS ...
user avatar
  • 3,794

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