2

I'm having a bit of a hard time wrapping my head around exactly how to work with both Relationships and Assets. Here is my current problem that I seem to be stumped on, i'm sure once I get this going I'll be good to go and fully understand how to work with both:

I need to determine if an entry is selected to be related to an entry single (the homepage) via the entry field 'featuredEvent', then I need to determine if the 'image' block is being used in the matrix 'articleBody' in that related entry, then I need to pull the first instance of the 'image' block (not necessarily the first block altogether), then I need to determine if there is more than one 'asset/image' in the 'image' field in the 'image' block in the related entry, then If there is only one image I need to output some code, and then output the image, as well as the other parameters 'size' & 'alignment', which are both drop downs, 'size' selects the pre-determined 'imageTransform' where as 'alignment' just applies a class or If there are multiple images i need to output some code, and then output the images, as well as the other parameters 'size' & 'alignment'.

Could anybody write that code out for me and explain what is happening in each line to help me get a better grasp? Thank you so much in advance.

  • 1
    What you're asking for is very involved. Rather than have us simply do all the work for you, could you edit the question with what you have thus far? Even if it's not working, we can instead help you fix what's wrong and explain more along the way. – Bryan Redeagle Jun 30 '14 at 20:24
  • I also recommend making your question more descriptive of what you're asking for in the details. – Bryan Redeagle Jun 30 '14 at 20:25
  • I've done a lot of trial and error and nothing has stuck since i don't quite grasp the way the data works yet. Hence asking to 'explain what is happening in each line' so i can get a grasp and be able to figure out other similar issues on my own. Not really sure how to be more descriptive, I tried to break it down literally item by item of what needs to happen. I have a single (the homepage) that has an 'entry' field to relate an entry and show it on the homepage. I need to then look in that entry for a particular matrix block type, grab the first of that type, and then the assets deal. – Christopher Healey Jun 30 '14 at 20:44
  • 1
    No worries. I'll explain as best I can. – Bryan Redeagle Jun 30 '14 at 20:48
13

The data in Craft is handled mostly by Models (and arrays of Models). Before pulling actual information, you define your criteria in an ElementCriteriaModel using generic parameters and parameters specific to what you are trying to get (entries, assets, etc.). You then use one of the ElementCriteriaModel's functions to pull the actual data. You will get either a model or an array of models to loop over. This works the same for most things in Craft because most things are Elements.

You can look under the Variables heading in the side menu of the templating docs to see the kinds of models you will get.

On your homepage, you start with an automatically created entry model (this is created on every page that Craft recognizes as an entry's page). Here's the basic code you need:

{% for event in entry.featuredEvent %}
    {# Output some event code here #}
    {% set block = event.articleBody.type('image').first() %}
    {% for image in block.image.find() %}
        {# Output some image code here #}
        <img src="{{ image.getUrl(image.size) }}" class="{{ image.alignment }}">
    {% endfor %}
{% endfor %}

So an explanation of the above. In Craft, related elements are not automatically pulled from the DB. Instead each field that points to a related element (entries, assets, categories, users, etc.) is an ElementCriteriaModel so that you add more criteria before pulling. So entry.featuredEvent is an ElementCriteriaModel that points to all of your featured event entries.

When working with ElementCriteriaModel you have a few options to pull the data and use it. In the above code, when you give a for loop ElementCriteriaModel it will automatically pull the data and give the loop entries. So we take the entry.featuredEvent, and loop over the entries that's given to us when we use it in the for loop.

Inside the for loop, we pull the first image block in event.articleBody. Because matrix blocks are elements, we get the same features. So we set the type, and pull the first one.

In the next for loop, we are grabbing all of our images in the image block. The tricky part here is that we can make the code a little more efficient by simply pulling all of the images and looping over them. If you have very different html you plan on using for one image and multiple images, you will need to set the images as a variable like we did the block. You can then check the number of images using the length filter, and either Craft's switch tag, or a couple if statements.

Personally, I recommend using the loop, and using CSS to make the style differences you need.

Finally, we output each image. Because you're using a transform on them, we can't use each assets getImg function. Instead we write the img tag manually and grab the transformed url using getUrl.

The next bit is making a couple assumptions. I'm assuming that size is a image transform handle. I'm also assuming that these fields are on the asset. If they're on the matrix block, replace image.size and image.alignment with block.size and block.alignment.

  • That means that the image field you mentioned isn't called image, or isn't an asset reference. It could also be a few other things, but I can't know without looking at it. – Bryan Redeagle Jun 30 '14 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.