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
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
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.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.