Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when overriding profile.tpl.php #35

Closed
robertgarrigos opened this issue Aug 24, 2024 · 14 comments · Fixed by #41
Closed

Error when overriding profile.tpl.php #35

robertgarrigos opened this issue Aug 24, 2024 · 14 comments · Fixed by #41

Comments

@robertgarrigos
Copy link
Member

Overwriting profile.tpl.php triggers an error:

Warning: Undefined array key "#entity" a template_preprocess_entity_plus() (línia 147 de /Users/robert/Sites/backdrop/modules/entity_plus/theme/entity_plus.theme.inc).
Error: Call to a member function label() on null a entity_label() (línia 774 de /Users/robert/Sites/backdrop/core/modules/entity/entity.module).

I found the same kind of error with paragraphs (backdrop-contrib/paragraphs#91) which has been fixed.

@argiepiano
Copy link
Contributor

argiepiano commented Aug 24, 2024

What do you mean by "overwriting"? Overriding?

Can you add more specific steps to reproduce? I believe you have Entity Plus enabled (which is not required for this module).

I do see some issues in profile.info.inc which is only used when E+ is enabled. For starters, it has the wrong callbacks: entity_property_verbatim_set and entity_property_getter_method, which don't exist. In Backdrop's Entity Plus, those should be entity_plus_property_verbatim_set and entity_plus_property_getter_method. So perhaps this is in part producing this problem (though I doubt it).

I see another issue, which may also be causing this problem: profile_entity_info() defines a 'label callback' => 'entity_class_label', which doesn't exist in Backdrop. It should probably be 'entity_label'.

If you want to try patching your site with those and seeing if that helps, that'd be helpful to pinpoint the problem.

@robertgarrigos
Copy link
Member Author

robertgarrigos commented Aug 25, 2024

Yes, overriding I meant. I need Entity Plus for some other module. Thanks, @argiepiano, those changes didn't fix the problem. Steps are easy, just create a profile and try to override profile.tpl.php file within your theme. Accessing the profile page triggers the error.

@argiepiano
Copy link
Contributor

Did you try clearing caches after the changes? If so, OK, that means that more debugging is needed.

@robertgarrigos
Copy link
Member Author

yep, cache was cleared.

@robertgarrigos
Copy link
Member Author

Commenting the function profile_entity_info_alter(&$entity_info) in profile.module fixes the error. I have been doing actions on profiles with no problem. Is that hook_entity_info_alter() function used at all?

@argiepiano
Copy link
Contributor

argiepiano commented Aug 25, 2024

Yes, it defines metadata for all of profile properties. You won't be able to use entity_metadata_wrapper with profile entities now. But this also points at the fact that the error is there. Most likely an inexistent callback, like I said above. You probably missed some of those.

To find the culprit, you should look at ALL callbacks specified there (as in setter callback , getter callback and others) and check that the callback function actually exists.

@argiepiano
Copy link
Contributor

Actually, what I just sald above is wrong. profile_entity_info_alter() does not provide metadata. It actually tells your site to use the controller provided by Entity Plus.

So, commenting that out will avoid using E+ entity controller for Profiles in your site. This is an acceptable solution in your case. But the bug needs to be found, since it still exists.

@robertgarrigos
Copy link
Member Author

I'm lost with this bug.

@argiepiano
Copy link
Contributor

If I had the time I'd try to reproduce ... not right now unfortunately.

@bugfolder bugfolder changed the title Error when overwriting profile.tpl.php Error when overriding profile.tpl.php Aug 25, 2024
@bugfolder
Copy link
Collaborator

bugfolder commented Jan 4, 2025

I can reproduce this. Created a profile, then added profile.tpl.php to Basis and cleared caches. Immediate error screen.

Now looking into it...

@argiepiano
Copy link
Contributor

argiepiano commented Jan 4, 2025

This problem happens because template_preprocess_entity_plus() expects the profile entity to be stored under #entity. In D7, this was done under DefaultEntityController::view(), the same as in B's EntityPlusController::view(). The problem here is that EntityPlusController::view() is never invoked - any calls to entity_view() now call Backdrop's core controller's view() method.

The solution is simple. We just need to pass the entity under #entity within Profile:view(). @bugfolder basically:

    $build += array(
      '#theme' => 'profile',
      '#profile' => $this,
      '#entity' => $this,
      '#view_mode' => $view_mode,
      '#langcode' => $langcode,
    );

notice the line #entity. This is in Profile::view().

I can provide a PR if you want

@bugfolder
Copy link
Collaborator

If we comment out the change of entity controller (as described above) and then view a user profile, we end up calling template_preprocess_profile($variables), and all is well.

However, if we leave the change of entity controller to use E+'s controller, then we don't call that preprocess function; instead, we call template_preprocess_entity_plus($variables), and get the crash. This is fundamentally because on line 147 of entity_plus.theme.inc, we have

  $entity = $variables['elements']['#entity'];

and there is no #entity element in $variables. The entity object actually does reside in $variables, but under the key #profile.

This seems to be set in Profile->view(), where we set up the array with this code at line 148 of profile.entity.inc:

    $build += array(
      '#theme' => 'profile',
      '#profile' => $this,
      '#view_mode' => $view_mode,
      '#langcode' => $langcode,
    );

Since $this is the entity object in question, I tried adding another copy of it to the array with the #entity key:

    $build += array(
      '#theme' => 'profile',
      '#profile' => $this,
      '#entity' => $this,
      '#view_mode' => $view_mode,
      '#langcode' => $langcode,
    );

This completely fixed the crash, and the user profile rendered correctly.

@argiepiano, based on your considerable entity guru-ship, does this sound like the appropriate change to make Profile module play nicely with E+?

@bugfolder
Copy link
Collaborator

Hah. Jinx.

@bugfolder
Copy link
Collaborator

Next time, I won't spend so much time writing up what I found!

bugfolder added a commit to bugfolder/profile that referenced this issue Jan 4, 2025
bugfolder added a commit that referenced this issue Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants