Jump to content

Is there a way to "concatenate" two images fields together ?


Recommended Posts

Disclaimer : it may be very dirty to do this and I'm pretty sure there are better solutions in the bigger picture. But I'm mostly just curious about this particular case. Here's the situation:

I'm creating a pretend portfolio.
My projects have a cover image (single image field) and a project images field (multiple images field)
In my project gallery I would like to include the cover image aswell as the other images. This can be easily solved by uploading the cover image aswell as the other images inside the multiple images field. But this creates a duplicate image file, which I'd like to eliminate.

Can I merge this single image with the multiple images field so that I don't have to upload the former twice ?

I also have the same scenario with file/files fields for video projects (would that work any differently ?).

Let's say these are my fields:

  $images = $item->images;
  $cover_image = $item->cover_image;

A caricature of what I'd like would be something like this:

  $all_images = $cover_image . $images;

But this quite obviously doesn't work, and neither does the following (tried to "fix" the resulting syntax, but this results in the output to no longer be an array anyway)

  $all_images = trim($cover_image . (cover_image ? '|' : '') . $images, '|');

That's pretty much it, curious if there's a simple solution to this, my searches were vain so far (I'm not sure of the correct terminology to call what I'm searching for, which doesn't help)

Link to comment
Share on other sites

Alternatively, you can just use different logic on your interactive procedure. I do something similar to this with just a single field, where the first image (manually sorted) in an images (Multiple Images field) is used as the primary/cover photo. All of the rest are placed into the standard gallery. The API makes that easy with the WireArray class methods of first() (only retrieve the first when using the "cover photo" as a portfolio thumbnail) and shift() (if you wanted to separate the first/cover photo from the rest of the group in a template).

Images are files, so you can use the same logic for file fields. If you don't want to modify your template(s) and would prefer to keep your logic flow intact, what dynweb mentions should work too. 🙂

  • Like 4
Link to comment
Share on other sites

@BrendonKoz Thanks for your reponse, yes this would absolutely be the reasonable solution in most cases, but the structure I have here is weird and it seemed relevant to have 2 separate fields at the time I created it. Maybe in hindsight I still should have gone for this solution, but it's just a personal project so I can't be bothered to change everything now, I'll have it learned for next time :^)

@dynweb
Thank you very much this is what I was looking for. Here's what I ended up doing, it works perfectly

  if ($cover_image) {
    $images = $images->prepend($item->cover_image);
  }

  if ($cover_video) {
    $videos = $videos->prepend($item->cover_video);
  }

 

  • Like 3
Link to comment
Share on other sites

I did something similar using image tags. I placed all the photos in an image gallery and assigned the 'cover' tag to the desired photo to be used as the cover. Then:

$image = $page->images->find("tags=mytag")->first();

My client actually loved this approach because we created new tags, and they can now assign them according to the position where the photo will be used

image.thumb.png.af736cc7e1d1a71351c37d48002ee9ac.png

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...