[ENHANCEMENT]: Missing places for class headers #1145

Open
opened 2024-07-17 15:06:00 +00:00 by vistamedia · 5 comments
Member

Enhancement Details

Hey,
I noticed a few places were it's not possible to add class headers. For some of them it's possible to find workarounds but for some others it's not.
Here are the places I found so far :

  • The installer script
  • The admin linked views
  • Cannot add class headers to default_body.php templates of the list views. class headers are added to the default.php only
    Cheers ;-)

Which part of the extension will be affected by this enhancement?

No response

Additional Context

No response

### Enhancement Details Hey, I noticed a few places were it's not possible to add class headers. For some of them it's possible to find workarounds but for some others it's not. Here are the places I found so far : - The installer script - The admin linked views - Cannot add class headers to default_body.php templates of the list views. class headers are added to the default.php only Cheers ;-) ### Which part of the extension will be affected by this enhancement? _No response_ ### Additional Context _No response_
vistamedia added the
enhancement
label 2024-07-17 15:06:00 +00:00
Owner

It is actually possible to add class headers everywhere. In fact, the current plugin that allows you to customly and manually target class headers in different views is obsolete and will be phased out when transitioning to Joomla 6. The new approach is what is called Super Power or Joomla Power Inclusion. These are two concepts that we will explore in depth at our next JUG meeting. I have already mentioned them in the past, but perhaps people have not fully understood their capabilities. This feature will automatically add your class headers anywhere in any area. All you need to do is use the SPK key in your custom code where you want the class name to appear.

Implementation of the SPK Key

In any place within your custom code where the class name should appear, you add the SPK key. For instance, if your class needs to be instantiated, you will use the keyword new followed by the SPK key as the class name.

$class = new Super___bfd1d6d5_56c1_4fe9_9fee_1c5910e1f5d8___Power();

This will result into:

$class = new Table();

Here's what happens:

  1. JCB will detect that this class must be added.
  2. It will add the use statement to the head of the file.
  3. JCB will intelligently detect if there are other classes with similar names and automatically adapt the use statement to avoid any collision.
  4. In the code, it will update the SPK placeholder with the correct class name.

This process is done per file, ensuring that each SPK placeholder is updated correctly every time. In fact, JCB already uses this intensively across the core of the application, and it has been working quite well since we moved to Joomla 5.

Use in Install Scripts

Using the SPK key in your custom code is the way to get the headers added to:

  • The install script.
  • The admin linked views.
  • The default body.

A little note about the install script: We've recently added a feature to the install script for Joomla 3, 4, and 5, which allows you to check whether the class is available. If the class is not available, it will trigger the built-in autoloader for the Joomla powers, then check if the class is available after that, and return true or false.

Here is the code to add the schema checker to any project:

// Check that the database is up-to date
if ($this->classExists(Super___709d7294_9a43_46e2_b64e_d16a16f0eab1___Power::class))
{
	(new Super___709d7294_9a43_46e2_b64e_d16a16f0eab1___Power())->run();
}

This is how it looks in JCB:
image

You need do nothing more, since after adding that code to JCB, it will auto detect these classes (search for them) add them to you that system (if they where not found in the database), add then to the Joomla Package and update all files as needed.

Be sure to use the latest package: https://git.vdm.dev/joomla/pkg-component-builder/archive/v3.2.2-beta2.zip (BETA-2)

This also works for the new Joomla Powers area, the stable version of that is part of the next release. Here is the repo with the docs. Please read over it, and give it a try... gone is the days, of struggling to keep up with the changes in Joomla, we can now per Joomla version drop in the new namespaces. With much more to come.

Demonstration

This is part of the demonstration that we plan to do in the next JUG meeting. I will provide more details during that demonstration on exactly how this is done. In short, this feature is already possible and quite powerful.

It is actually possible to add class headers everywhere. In fact, the current plugin that allows you to customly and manually target class headers in different views is obsolete and will be phased out when transitioning to Joomla 6. The new approach is what is called Super Power or Joomla Power Inclusion. These are two concepts that we will explore in depth at our next JUG meeting. I have already mentioned them in the past, but perhaps people have not fully understood their capabilities. This feature will automatically add your class headers anywhere in any area. All you need to do is use the SPK key in your custom code where you want the class name to appear. ### Implementation of the SPK Key In any place within your custom code where the class name should appear, you add the SPK key. For instance, if your class needs to be instantiated, you will use the keyword `new` followed by the SPK key as the class name. ```php $class = new Super___bfd1d6d5_56c1_4fe9_9fee_1c5910e1f5d8___Power(); ``` This will result into: ``` $class = new Table(); ``` Here's what happens: 1. JCB will detect that this class must be added. 2. It will add the `use` statement to the head of the file. 3. JCB will intelligently detect if there are other classes with similar names and automatically adapt the `use` statement to avoid any collision. 4. In the code, it will update the SPK placeholder with the correct class name. This process is done per file, ensuring that each SPK placeholder is updated correctly every time. In fact, JCB already uses this intensively across the core of the application, and it has been working quite well since we moved to Joomla 5. ### Use in Install Scripts Using the SPK key in your custom code is the way to get the headers added to: - The install script. - The admin linked views. - The default body. A little note about the install script: We've recently added a feature to the install script for Joomla 3, 4, and 5, which allows you to check whether the class is available. If the class is not available, it will trigger the built-in autoloader for the Joomla powers, then check if the class is available after that, and return true or false. Here is the code to add the schema checker to any project: ```php // Check that the database is up-to date if ($this->classExists(Super___709d7294_9a43_46e2_b64e_d16a16f0eab1___Power::class)) { (new Super___709d7294_9a43_46e2_b64e_d16a16f0eab1___Power())->run(); } ``` This is how it looks in JCB: ![image](https://git.vdm.dev/attachments/cd74e30f-6bc8-4fc5-9195-307ea070773d) You need do nothing more, since after adding that code to JCB, it will auto detect these classes (search for them) add them to you that system (if they where not found in the database), add then to the Joomla Package and update all files as needed. Be sure to use the latest package: https://git.vdm.dev/joomla/pkg-component-builder/archive/v3.2.2-beta2.zip (BETA-2) This also works for the new **Joomla Powers** area, the stable version of that is part of the next release. Here is the [repo with the docs](https://git.vdm.dev/joomla/joomla-powers). Please read over it, and give it a try... gone is the days, of struggling to keep up with the changes in Joomla, we can now per Joomla version drop in the new namespaces. With much more to come. ### Demonstration This is part of the demonstration that we plan to do in the next JUG meeting. I will provide more details during that demonstration on exactly how this is done. In short, this feature is already possible and quite powerful.

@Llewellyn I know it's not the place, but can you tell me more about the JUG, you are talking about...?
Where and when and how to access it...
Thanks

@Llewellyn I know it's not the place, but can you tell me more about the JUG, you are talking about...? Where and when and how to access it... Thanks
Owner
See https://joomlacommunity.cloud.mattermost.com/main/channels/jcb for more details ;)
Author
Member

Thanks for this extensive answer Llewellyn. I understand now what I can achieve with super powers. It solves 95% of my issue.
The only remaining problem is how to use my component helper class in a post-install method ?

Thanks for this extensive answer Llewellyn. I understand now what I can achieve with super powers. It solves 95% of my issue. The only remaining problem is how to use my component helper class in a post-install method ?
Owner

The following will only work for Joomla 4+ (components) and build with:

The easiest way would be for the ADMINISTRATOR helper (in the install script):

// Check that the helper class is loaded
if ($this->classExists(Joomla___aebfeb9f_f8a3_42be_a21d_5db56ae30c1c___Power::class))
{
	Joomla___aebfeb9f_f8a3_42be_a21d_5db56ae30c1c___Power::helperMethod();
}

The easiest way would be for the SITE helper (in the install script):

// Check that the helper class is loaded
if ($this->classExists(Joomla___cf3b95c1_ba56_4193_8eb4_9164582b7f55___Power::class))
{
	Joomla___cf3b95c1_ba56_4193_8eb4_9164582b7f55___Power::helperMethod();
}

This will add the component helper class to the head of the file, and allow you to call it. I have not tested this on a new install yet... let me know.

The following will only work for Joomla 4+ (components) and build with: - [JCB v5.0.1-beta5+](https://git.vdm.dev/joomla/Component-Builder/archive/5.x.zip) - [JCB v4.0.1-beta5+](https://git.vdm.dev/joomla/Component-Builder/archive/4.x.zip) - [JCB v3.2.2-beta5+](https://git.vdm.dev/joomla/Component-Builder/archive/3.x.zip) The easiest way would be for the ADMINISTRATOR helper (in the install script): ``` // Check that the helper class is loaded if ($this->classExists(Joomla___aebfeb9f_f8a3_42be_a21d_5db56ae30c1c___Power::class)) { Joomla___aebfeb9f_f8a3_42be_a21d_5db56ae30c1c___Power::helperMethod(); } ``` The easiest way would be for the SITE helper (in the install script): ``` // Check that the helper class is loaded if ($this->classExists(Joomla___cf3b95c1_ba56_4193_8eb4_9164582b7f55___Power::class)) { Joomla___cf3b95c1_ba56_4193_8eb4_9164582b7f55___Power::helperMethod(); } ``` This will add the component helper class to the head of the file, and allow you to call it. I have not tested this on a new install yet... let me know.
Sign in to join this conversation.
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: joomla/Component-Builder#1145
No description provided.