In a custom module I must assign a value to the name field programmatically.
This value is obtained from the concatenation of the values of two fields. One of these fields is of relational type and when trying to obtain its value in the before_save
and after_save hooks
I get null
so I cannot use it for the desired purpose.
This works if I edit a custom module record. Specifically in the after_save
hook
I thought I could run a scheduler
, which would look for the records with name field empty and assign a value
Is there another approach to achieve the desired behavior?
Share the full code of your hook. It’s likely that you simply need to load_relationship before you can use the field value.
Thanks for consulting. I have achieved the expected behavior using the before_save
and after_relationship_add hooks
Implemented as follows
public/legacy/custom/modules/<custom module>/logic_hooks.php
<?php
$hook_version = 1;
$hook_array = [];
$hook_array['before_save'] = [];
$hook_array['after_relationship_add'] = [];
$hook_array['before_save'][] = [
77,
'Set name',
'custom/modules/<custom module>/hooks/CommentHook.php',
'CommentHook',
'beforeSave'
];
$hook_array['after_relationship_add'][] = [
79,
'Set name',
'custom/modules/<custom module>/hooks/CommentHook.php',
'CommentHook',
'afterRelationshipAdd'
];
public/legacy/custom/<custom module>/hooks/CommentHook.php
<?php
class CommentHook
{
public function beforeSave($bean, $event, $arguments): bool
{
$bean->load_relationship('comment_artists');
$performances = $bean->comment_artists->get();
if (!count($performances)) {
return true;
}
$performanceBean = BeanFactory::getBean('artists', $performances[0]);
$bean->name = $performanceBean->name . ' '. $bean->section;
return true;
}
public function afterRelationshipAdd($bean, $event, $arguments): bool
{
$performanceBean = $arguments['related_bean'];
$bean->name = $performanceBean->name . ' '. $bean->section_c;
return true;
}
}
I hope it is useful to someone
1 Like