Questions tagged [array]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
12
votes
4answers
8k views

What is the best way to exclude an array of entries?

I have done a "Is featured" switch on articles which works great. I can list the latest 4 featured articles but after that I would like to list the others (Of course they may be featured too)...
8
votes
1answer
37k views

Creating dynamic array and set keys

I have content which needs to go into a certain column so I have a field called 'column' there. I can create the array I want by hand: {% set items = { 1 : { 0 : 'Quick Facts',...
6
votes
3answers
2k views

How do I find live and pending entries when using an array?

I have this in my listings template, but the 'pending' parameter does not seem to work. {% set urlCat = craft.request.getSegment(3) %} {% set category = craft.categories.slug( urlCat ) %} {% set ...
5
votes
4answers
15k views

Overwrite value in object in array

I'm strangely having trouble with this simple Twig merge problem and can't find anything online. Simplified, let's say you had: {% set users = [ {name: 'Bill', age: 25}, {name: 'Tim', age: 30}...
5
votes
2answers
20k views

Using Twig to merge an array and remove duplicates

Problem I'm building a .json file for a search component of a used cars website. The file needs to be built from the output of two sections within CraftCMS, Used Cars and Vans & Trucks. It ...
5
votes
2answers
32k views

Is there a way to check if a value exists in an array in twig?

Done some searching on this but can't quite find what I'm looking for. Most threads are regarding keys. I want to know if there is a way to test if a value exists in a twig array. It should be able ...
4
votes
1answer
95 views

Store categories in array by level in controller

I'm making a plugin and I need to get an array from a certain category group where the levels of the array are the same as the level of the categories in the group. So I used the following code to ...
3
votes
1answer
674 views

Alphabetical index for a list of entries

I would like to build an alphabetical list of craft entries, but group them by the initials of one field of each entry. Thus, getting some kind of alphabetical index, like A Apples Ananas B Beer ...
3
votes
2answers
147 views

Complicated A-Z problem

I am attempting to create an 'a to z' listing on a site with 1000+ entries of people. The catch is that it needs to be indexed by surname unless that person doesn't have a surname, in which case it ...
3
votes
2answers
1k views

Adding items to an array

Is it possible to insert items into specific positions in an array (rather than at the beginning or at the end), a bit like javascript splice? Say, if I wanted to add a value between items 2 and 3? ...
3
votes
1answer
502 views

Entries & Twitter Posts

I'm trying to build what's basically a blog page, but spread throughout the posts we want some of our tweets as well. Obviously we have the entries from Craft, and I've found this plugin which I can ...
3
votes
1answer
184 views

Pagination / Custom Routes (Based on Entry Existing)

I've created a custom route for my entries based on week like so: 'events/(?P<year>\d{4})/(?P<week>\d{2})' => 'events/_weekindex', I know i can group entries by week like this: {% ...
3
votes
1answer
2k views

Possible to merge and sum the values of an array?

I have a super table that has 4 columns. I'm trying to create an array based off one column (contentType) and merges to get only the unique values while taking the sum of a different column (points). ...
2
votes
2answers
5k views

Adding entries to an array, one at a time

I'm trying to loop through some entries and if they fulfil a certain condition push them into another array of entries. {% set todaysEvents = [] {# GET TODAY'S ONE-OFF EVENTS #} {% set ...
2
votes
2answers
963 views

How can I iterate through an array of field handles for an entry?

Background: One of my templates has up to 6 fields that should each go into the same markup. Here's an example with a courseDesign field: {% if entry.courseDesign|length %} <section class="col-...
2
votes
2answers
2k views

Random Number from range

What is the best way to get a random number within a certain range? I made something but I guess there is a better way: In this example I want to position some elements randomly. This approach works,...
2
votes
1answer
2k views

Conditional to detect mobile devices with getUserAgent?

I want to create a User Agent check. (Yes I know. It is evil.) So I created an Array. The whole idea is to see if the user agent contains a specific word. This way I hope to circumvent regex trickery....
2
votes
1answer
705 views

Shouldn't this merge be working?

Can anyone help me work out why the following returns the error "The merge filter only works with arrays or hashes."? {# CREATE ARRAY TO HOLD ALL OF TODAY'S EVENTS #} {% set todaysEvents = [] %} {# ...
2
votes
1answer
387 views

List Categories by 0-9 and A-Z

I'm trying to output a list of Brand names grouped by first letter. But some items have a number or symbol in the name. I want to list first the Numbers and symbols under 0-9 and the rest of the ...
2
votes
1answer
147 views

How to build a sentence based on tags

I try to build a complete sentence based on tags as follows: This is a sentence with tag1, tag2 and tag3. My code is: {% set topics = entry.topics.first %} {% for topic in entry.topics.offset(1) %}...
2
votes
2answers
194 views

How can I tell if a variable is an array or an entry/element query?

We have some components that are used to build pages, like normal, and some where we hydrate them with other data, so we are passing in arrays instead of entries. If something is essentially an ...
2
votes
2answers
296 views

Updating Array Variable In Extending Template

I have a layout template: {% set bodyClass = [] %} <body class="{% for class in bodyClass %}{{ class }} {% endfor %}"> {{ block('pageBody') }} </body> And a template that extends it:...
2
votes
2answers
446 views

Is it possible to convert plain text array to an unordered list?

I have a front end form with checkboxes which submits the data as an array into a plain text field in craft (named logGeneral_logInputIndications). Here's an example of what the content looks in the ...
2
votes
2answers
64 views

Can I exclude a commerce address by ID when retrieving a list of addresses?

I would like to retrieve a list of all commerce customer addresses except for one. When working with entries, this can be done with the {id: 'not ' ~ some.id } parameter. This doesn't seem to work ...
2
votes
2answers
111 views

Merge, shuffle, and paginate 😱

I'm trying to get a list of users from two different user groups. One user group (premium) must always show first. Whenever the page is loaded, however, the listing of users should show randomly (...
2
votes
1answer
142 views

Check for number less than x in array

I want to check if there is a number less than 10 in an array. {% if 10 in [0, 30, 50, 10, 9, 200] %} {# checks only if there is a 10 in it. not if there is anything below 10. #} yep. there is ...
2
votes
1answer
1k views

Iterate through all entries and count a specific value

I am having entries in a Craft channel-section. Each entry has the field "type" with one category filled. I need to iterate through the entries and count the amount of each category. Example: Entry ...
2
votes
1answer
91 views

Adding a random entry to an array

I have a navi on which one entry consists of a random channel entry from one section among standard singles: Single 1 Single 2 Random Channel entry Single 3 I thought about something like this: {% ...
2
votes
1answer
143 views

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

Two channels: 1) exhibitions, which have a startDate and EndDate 2) events, which have a date I need to grab the current exhibitions, future exhibitions and future events, which I do using this: {% ...
2
votes
1answer
717 views

Eager-load and pagination

I'm currently working on a relation-heavy website with a colleague of mine and, for that reason, eager-loading stuff looks very appealing to us. We've succeeded in eager-loading pretty much everything ...
2
votes
1answer
2k views

Create an object of objects with merge

I'm trying to create an object of objects, can anyone suggest why object would only contain the last iteration of "itemObject"? {% set object = {} %} {% for entry in craft.entries.section("...
2
votes
1answer
917 views

Empty .getParam() does not return an array with dump()

craft.request.getQuery() returns: array (size=5) 'tags' => string 'anxiety' (length=7) 'maxPrice' => string '' (length=0) 'sessions' => array (size=4) 0 => string 'email' ...
2
votes
1answer
2k views

Filter out duplicate entries in array?

I have some matrix fields containing a date. I then loop through the entries in a category looking for entries containing a matrix field with a date during the next month. Everytime a matrix field is ...
2
votes
1answer
3k views

Array to string conversion Template Error

I have 2 bits of code that should be about identical with the exception of one being in a Matrix and one not. The bit that is not looks like this: <ul> {% for point in entry.keyPoints %} ...
2
votes
1answer
31 views

Problem with truncated Freeform checkbox group value when mapped to another field

NB: this is not the same as Freeform Checkbox Group Field only returns one value, which was resolved by field name markup invoking an array: name="fieldname[]". I'm already doing that. And ...
2
votes
1answer
266 views

Remove duplicated entries from SuperTable related entries

Anyone able to assist me with removing duplicates in this example? So I have a related entries (cables) setup in a SuperTable. Some of these entries are duplicated across different rows. So a cable ...
2
votes
1answer
118 views

Insert entry into article field (elementselect?) on a user, with the "onBeforeSaveEntry" hook

I'm having a hard time inserting new content into an elementselect field for a user. Here's the code I'm working on so far, inside a custom plugin init() parent::init(); craft()->on('entries....
2
votes
1answer
639 views

How to add to arrays in models?

I have a model that contains a bunch of storage arrays where I collect results from various services I have written. class Shipper_ResultsModel extends BaseModel { shipping categories for debugging ...
2
votes
1answer
253 views

Selecting categories by first letter

I'm attempting to pull out a list of categories starting with certain letter(s). What I've come up with so far seems a little excessive and I'm wondering if I've missed something obvious. Can it all ...
2
votes
1answer
173 views

Order a list of categories by number of entries, key not being set

I have modified the code from this question but I'm running into an issue. {% set allCats = craft.categories.group('blogCategories').level(1).find() %} {% set catArray = {} %} {% for cat in ...
2
votes
1answer
41 views

Build array from nodes from RSS where keys contain colons

Probably pure twig: I am building an array from nodes taken from a RSS-feed where one of the keys contains a colon which of course breaks everything. Like so: Node in feed: <media:thumbnail url=...
2
votes
1answer
167 views

How to display entries filtered from more than one category

I want to try to learn how I can filter entries by multiple categories. I realise similar questions have been asked in the past, but all the answers are for specific use cases. All I want to do is ...
2
votes
1answer
231 views

Load entries based on category field value while multiple categories are selected

I have an activity calendar (more like a weekly schedule) which filters per day and per category. Because there are so many activities I can't just load them all in at once so I use some sort of Ajax ...
2
votes
0answers
410 views

Checking if a value matches an entry type handle

I am trying to check if a value provided by the user (url segment) matches an entry type, and if not redirect to 404. {% set section = craft.sections.getSectionByHandle('shop') %} {% set entryTypes = ...
1
vote
2answers
44 views

Checking if a value is in an array of IDS - using a filter?

Is there an easier way to achieve this? Perhaps using a |filter function? {% set variantIds = [] %} {% for variant in product.variants %} {% set variantId = variant.id %} {% set variantIds = ...
1
vote
2answers
75 views

Merging classes in Twig based on component and context

Whats the best way to approach this in twig? So I have a component that is used in multiple places, so within this component I set some default classes like so: {% set navSidebarClasses = { heading: ...
1
vote
1answer
2k views

Impossible to invoke a method ("limit") on an array... but it's not an array?

I am receiving the error message "Impossible to invoke a method ("limit") on an array," but I don't think I am working with an array! Here is why I think that... First I set an albums variable: {% ...
1
vote
1answer
58 views

Pagination and Entry Model on Homepage

On my site I have some entries with images. In the entry template I paginate the images like so: {% paginate entry.images.limit(1) as images %} My homepage is a single random entry and I want to ...
1
vote
1answer
1k views

Returning an array to Twig from a plugin

I'am writing a plugin that has to return an array of data, however Twig does not seem to accept it if I return a PHP array. I don't want to use an explode in Twig to make it into an array. I have ...
1
vote
1answer
2k views

Apply 'random' to an array

How do I output one random line of an array? {% set img = [ { bildli: 'http://i.giphy.com/I0xDz0htjsiME.gif' }, { bildli: 'https://i.giphy.com/fA3JALhIi5YVa.gif' } ] %} {% for item in img %} ...