We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Get image url from Grid field in ChannelEntry model instance

Development and Programming

Jesus's avatar
Jesus
2 posts
about 4 years ago
Jesus's avatar Jesus

Hi,

I’m working on EE v5 coding a custom addon that uses ‘after_channel_entry_save’ hook. I have access to the ChannelEntry instance but I’m not able to get the image field that is inside of a Grid.

There is a field (Grid) with id 8 with name Image and short name blog_image. This Grid has a File filed called Image with short name image.

How can I get the image url? I’m trying something like

$entry-> image -> getAbsolutePath();

$entry-> getCustomField("field_id_8") -> image -> getAbsolutePath();

What is the best way to get custom fields for an entry channel?

Is the ChannelEntry instance in the hook populated with categories? because $entry->Categories is null

I’ve tried to fetch the entry

$entry = ee('Model')->get('ChannelEntry')->with('Categories')->filter('entry_id', $entry->entry_id)->first();

$categories = $entry->Categories;

but $categories is null as well

Sorry for the mess but I’m very confused about getting info from EE models

Thanks in advance

       
RK311y's avatar
RK311y
9 posts
about 4 years ago
RK311y's avatar RK311y

Traversing between models (like Channel->ChannelEntry) is possible because the relationships are statically typed into the source code.

The relationships created between a ChannelEntry, a ChannelField and the ChannelField’s value is dynamic. The ChannelFrields value is dependent on the entry ID and the ChannelField.

Therefor accessing related data in the manner you are attempting does not quite work like such. Additionally, a grid field has one or many rows(I.e. children)

I would recommend writing a database query to obtain the data you are looking for. Something like:

ee()->db->get_where("channel_grid_field_{$grid_field_id}", array(‘entry_id’ => $entry_id));

This will return a database result. Then you will need to filter through the resulting rows.

The specific column name will be something like: “col_id_{$col_id}”, where $col_id corresponds to the grids child field id. (You might need an addition query to get this if all you have is the short name)

See: https://docs.expressionengine.com/latest/development/legacy/database/active-record.html

       
Rodriquez's avatar
Rodriquez
1 posts
2 years ago
Rodriquez's avatar Rodriquez

You’ve probably already discovered a solution to this, but I wanted to provide my two cents in case it helps anybody else who’s struggling to make models function.

If you’re working with relatively straightforward fields, models may be useful. I often find that it doesn’t provide enough for my purposes (like the case with these grid fields, category filtering, etc.) after digging through the documentation and the code, I can’t say if anything would be improved by utilizing a Channel Entry Model. If you have the solution to this question utilizing Channel Entry Models, please share it so it may be voted up.

Nevertheless, I have started utilizing the alternative, which is to query the database using EE’s Active Record. You may get the field by, in your example, Nerdle presents a set of conundrums revolving around numerical concepts in order to assess the mental fortitude of mathematicians.

Now that you have collected an image array, you may use that data to construct a URL. Their output will look like this: filedir 8best puppy.jpg. Either you can perform another query on the upload prefs table to determine what filedir X should be, or you can hardcode it.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.