I'm working on a custom module to update a field "curryear_birthdate" in an entity "Person" in my Drupal 10.2.4 site using the hook_ENTITY_TYPE_presave() construct. I followed the template that several Drupal guides showed.
I have two problems:
1) The field doesn't get updated; and
2) I put a debugging display in to see what might be the problem and the display is not appearing.
The code for the .module is shown below.
<?php
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_ENTITY_TYPE_presave() for person entities.
*/
echo '<script>alert("Got into birthdaycurryear.module")</script>';
function curryear_birthdate_Person_presave(Drupal\Core\Entity\EntityInterface $entity) {
echo '<script>alert("Got into curryear_birthdate_Person_presave function")</script>';
if ($entity_type->get('type') === 'Person' && $entity->hasField('Birthdate of person')) {
$birthdate = $entity->get('Birthdate of person')->date;
$dateAsStr = $birthdate->format('Y-m-d-H-i-s');
echo '<script>alert("Birthdate = ".$dataAsStr)</script>';
if ($birthdate) {
$current_year = date('Y');
$month = $birthdate->format('m');
$day = $birthdate->format('d');
$curryear_birthdate = new DateTime("$current_year-$month-$day");
$entity->set('curryear_birthdate', $curryear_birthdate->format('Y-m-d'));
}
}
}
The first display [echo '<script>alert("Got into birthdaycurryear.module")</script>'; ] works fine. Every time I do a step the pop-up display shows, so I know that the system knows that my module is around and installed.
But, my second display [echo '<script>alert("Got into curryear_birthdate_Person_presave function")</script>'; ] never shows.
I anticipated that any time I tried to update a Person record that second display would show, but it never does.
So, either:
1) echo displays within functions don't work the way I think they would;
2) The system never triggers the curryear_birthdate_Person_presave() function; or
3) There's a syntax error that the system is not telling me about
I usually assume that I'm missing something simple.
I'd appreciate anyone telling me what that "something simple" might be in this case.
Thanks!
Comments
Is Person maybe a content
Is Person maybe a content type, rather than an entity type?
Contact me to contract me for D7 -> D10/11 migrations.
Content vs entity
Person is a content type.
I was thinking that might be a potential issue which is why I put the "echo ..." display in within the function so I can could confirm what it was looking for.
That doesn't [I don't think] explains why my "echo..." display is not popping up.
Do you have a suggested change to my syntax?
Thanks for all your help.
Content types are all
Content types are all entities of type Node. Person is the bundle type, not the entity type. So your hook would be:
curryear_birthdate_node_presave().Also, echoing things is not a great way of debugging within Drupal, due to various caching, and redirects and this and that. At the least, you are better putting out a message:
\Drupal::messenger()->addStatus('some message');Contact me to contract me for D7 -> D10/11 migrations.
Messages and nodes
Thanks for your note and suggestion.
Things still don't work.
Here's the environment for the Drupal 10.2.5 site.
Custom Module name: birthdaycurryear.module
I want to update instances of content type Person when they get added or modified.
I've greatly simplified the module to just see if it is ever called. I'll worry about what I want the module to do later.
The module, based on your recommendation, is:
<?php
/**
*
* Implements hook_ENTITY_TYPE_presave() for person entities.
*
*/
use Drupal\Core\Entity\EntityInterface;
function birthdaycurryear_node_presave(EntityInterface $entity) {
\Drupal::messenger()->addStatus('Got into birthdaycurryear_node_presave function');
}
As I mentioned before, I've tried various approaches to get a "I made it into the function" message to display or get into the log, but nothing has ever been displayed or stored, so I don't think the system ever calls and executes the function.
Is it the "Implements the hook_..." comment that triggers things or the function name?
I was assuming that it was the function name. Is that correct?
If I put a "I made it into the module" message into the code before the function is defined, that message gets displayed, so I know that the module got enabled and installed correctly.
I've tested adding a new Person and editing an existing Person.
What else can I try to diagnose what's going on?
Did you clear your registry
Did you clear your registry after adding that function? Hooks are cached, and need a cache clear to be picked up.
Is your module installed?
Is your module key birthdaycurryear? Is your info file named birthdaycurryear.info.yml?
Yes, it is the function name, not the comments. Although in other parts Drupal the comments do get picked up. But not for hooks, for hooks it is only the function name.
Contact me to contract me for D7 -> D10/11 migrations.
Clear as ...
I cleared the cache and the module suddenly "work up" and is displaying the message.
Now that I know that the system and the module are talking with one another, I can now put in some code to have the module actually do something.
Thanks for your patience with all my questions.
Thanks again
I just wanted to thank you again for your patient assistance.
My custom module finally is working and doing what it's intended to do.
Developing it was certainly a good educational course on the concepts involved. And I even learned how to do display messages efficiently along the way.
Thanks again!
All steps in becoming a
All steps in becoming a Drupal developer! Glad you got it sorted out.
The last thing I'll add is that in the future, when posting code here on the forums, you can/should format it as code. You can do that using the button in the toolbar that looks like a sheet of paper with <> on the bottom right. Clicking that opens up a modal where you can paste your code, and it will be formatted as code when you submit your post.
Contact me to contract me for D7 -> D10/11 migrations.
Formatting code
Thanks.
It's good to learn what the proper way of doing things is. I appreciate your assistance.
Urban