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

Introduce predefined path in plugins, modules, templates for custom fields in the manifest file #4294

Closed
wants to merge 25 commits into from
Closed

Conversation

dgrammatiko
Copy link
Contributor

Simple addition to allow components, plugins, modules and templates to use custom fileds in a sub-directory named fields.

This is an alternative way to use custom fields in these areas besides the classic hardcoding addfieldpath=“wherever". Should make things a bit easier for devs without introducing complicated and slow procedures (only one directory check is actually used).

Pros

Predefined workflow
Easier implementation
No need for hardcoded paths in xml
Unified method that works along the existing hand coded addfieldpath=“wherever"

Against

Adds one more directory with predefined name
Adds some more calls for every template, module, plugin in the pile of execution code.

There shouldn't be any B/C breaks here.

How to test

For plugins:
apply this patch
apply also patch #4240
open /plugins/editors/tinymce/tinymce.xml and remove:
addfieldpath="/plugins/editors/tinymce/fields”
from the line:
<fieldset name="basic" addfieldpath="/plugins/editors/tinymce/fields”>
Go to admin and observe that the fields site skins and administrator skins are still functional.

For templates check the instructions at #4295

For modules repeat the instructions of plugins but for any module you choose. For a custom field code just use the one from the tinymce and it should work (you only need to see it rendering the functionality is not essential here). So for mod_breadcrumbs:
open mod_breadcrumbs.xml and paste
<field name="skin_admin" type="skins" description="PLG_TINY_FIELD_SKIN_ADMIN_DESC" label="PLG_TINY_FIELD_SKIN_ADMIN_LABEL" > </field>
under the fieldset=“basic”
copy the folder fields from the plugin tinimce [the plugin test abobe]
check in the admin module breadcrumps for the new option

For components:
This is already the standard for components. Custom fields expected to be found at com_name/models/fields

Also try that 3PD plugins, modules and templates don't misbehave!

Simple addition to allow plugins to use custom fileds in a directory named ```fields```.
There shouldn't be any B/C breaks here.
@dgrammatiko dgrammatiko changed the title Allow custom fields in plugins Allow custom fields in plugins installation file [xml] Sep 17, 2014
@Bakual
Copy link
Contributor

Bakual commented Sep 18, 2014

I'm not sure what the purpose is. It's already possible to use custom fields by providing the addfieldpath to the XML.
If I understand that correct, you just want to automate this so you don't have to define the path yourself?

@dgrammatiko
Copy link
Contributor Author

@Bakual three things are done with this simple line of code:

  1. Code doesn't need to be hard coded in xml's files
  2. We get a unified way to do things
  3. It makes things easier for dev's

@Bakual
Copy link
Contributor

Bakual commented Sep 18, 2014

Code doesn't need to be hard coded in xml's files

Hmm, so instead of "hardcoding" the path it in the XML, we hardcode it in the model.

We get a unified way to do things

Actually, you introduce a new way to do things. Now one could either use the predefined path or specify an own in the XML. But only in plugins and templates. Components and modules would need additional PRs to be in line with this change, right?

It makes things easier for dev's

A tiny bit, yes. However I have to say it's very simple to add this attribute to the XML. This option needs to stay anyway, because extensions may have their fields in a library or other places.

I'm not sure if I like it or not. It's only a tiny gain imho at the cost of adding a tiny bit of additional complexity to the system. Let's see what others think.

Can you maybe adjust the title and description of the PR? Currently it's misleading as it has nothing to do with allowing custom fields for plugins (or templates). That is already possible without any issues by providing the addfieldpath attribute in the XML.

@dgrammatiko dgrammatiko changed the title Allow custom fields in plugins installation file [xml] Introduce predefined path in plugins xml file for custom fields Sep 18, 2014
@dgrammatiko
Copy link
Contributor Author

@Bakual

Hmm, so instead of "hardcoding" the path it in the XML, we hardcode it in the model.

I put it as a new more standardized workflow, but that maybe only my opinion 😃

Now one could either use the predefined path\

And there is gonna be another predefined path /fields in the current plugin.

It's only a tiny gain imho at the cost of adding a tiny bit of additional complexity

It’s not that complex, imho, the admin components just have a check if the plugins/type/name/fields exist and if so add it as a fieldpath.

The concept is simplicity, transparency and unification but to be honest I don’t know if the outcome justifies that. More options here are welcome

@dgrammatiko dgrammatiko changed the title Introduce predefined path in plugins xml file for custom fields Introduce predefined path in plugins, modules, templates xml file for custom fields Sep 18, 2014
@dgrammatiko dgrammatiko changed the title Introduce predefined path in plugins, modules, templates xml file for custom fields Introduce predefined path in plugins, modules, templates for custom fields in the xml file Sep 18, 2014
@dgrammatiko dgrammatiko changed the title Introduce predefined path in plugins, modules, templates for custom fields in the xml file Introduce predefined path in plugins, modules, templates for custom fields in the manifest file Sep 18, 2014
@sovainfo
Copy link
Contributor

Don't understand what you meant to say. Do see that you don't want to use JPATH_COMPONENT_ADMINISTRATOR for some reason. From a programming point of view consider your code wrong. The constant JPATH_COMPONENT_ADMINISTRATOR is declared a few lines up, you have a chance to use it, but choose to recalculate it. Apart from the fact it is inconsistent it is bad programming!

@sovainfo
Copy link
Contributor

No, use JPATH_COMPONENT_ADMINISTRATOR in both if and addFieldPath instead of calculating it twice!

@dgrammatiko
Copy link
Contributor Author

@sovainfo Honestly is late, you are right I PR the change

@sovainfo
Copy link
Contributor

tried to do that as well, can't pr your pr!

@dgrammatiko
Copy link
Contributor Author

@sovainfo Did I get it right now? 😕

@asika32764
Copy link
Contributor

Nice change 👍

@@ -322,6 +322,21 @@ public static function renderComponent($option, $params = array())
throw new Exception(JText::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'), 404);
}

if (JPATH_COMPONENT == JPATH_COMPONENT_ADMINISTRATOR)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't a simple

if (is_dir(JPATH_COMPONENT . '/models/fields'))
{
    JForm::addFieldPath(JPATH_COMPONENT . '/models/fields');
}

do the same?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, would prefer that over the current lines 325/339

@dgrammatiko
Copy link
Contributor Author

Some documentation updates needed if this gets approved.
These are the ones I found:
http://docs.joomla.org/Form_field
http://docs.joomla.org/Creating_a_custom_form_field_type
http://docs.joomla.org/Creating_a_modal_form_field

@@ -555,6 +555,8 @@ public function getForm($data = array(), $loadData = true)
$id = JArrayHelper::getValue($data, 'id');
}

JForm::addFieldPath(JPATH_BASE . '/modules' . '/' . $module . '/fields');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work that easy. JPATH_BASE will be pointing to admin when the module is edited in backend and to site when it is edited in frontend. At the same time we have both backend and frontend modules.
So what you need here is load the folder depending on the $clientId:

$baseFolder = ($clientId) ? JPATH_ADMINISTRATOR : JPATH_SITE;
JForm::addFieldPath($baseFolder . '/modules' . '/' . $module . '/fields');

@sovainfo
Copy link
Contributor

Disagree on removing the path existence check. Considering the amount of processing don't think it is a good idea to introduce a lot of paths that don't exist. It does take memory and cpu, why waste it?
My definition of efficient and DRY is different, I guess! It doesn't make sense to me to tell looking at places that don't even exists.
To be consistent those checks should be done for the component as well.

@Bakual
Copy link
Contributor

Bakual commented Sep 19, 2014

Disagree on removing the path existence check. Considering the amount of processing don't think it is a good idea to introduce a lot of paths that don't exist.

I'm sure you will not be able to measure the difference. It's only a single path added. We also do a lot more such path checks for our override system, class autoloading and many more. It's not something which uses lot of ressources to process.

On the other hand, adding is_dir checks makes the code more complex and less readable.

@dgrammatiko
Copy link
Contributor Author

@Bakual @sovainfo So what’s the decision here, besides the admin/front end path definition?

@sovainfo
Copy link
Contributor

Can't take those arguments seriously. But have no interest to continue that discussion with someone I consider unreasonable.

@dgt41 Apologize for assisting you, looks like my input is not appreciated and will cause it not to be implemented. Sorry, maybe there are others that do understand the importance.

@dgrammatiko
Copy link
Contributor Author

@sovainfo On the above argument I think what @Bakual says, and he is correct, is that if we do a path existence check here we are gonna repeat the procedure when we actually gonna add the fields. On the other hand the majority of modules, plugins and templates don’t use custom fields, so maybe it will be beneficial not to introduce them in the first place, and thus save some resources. Both ways are fine for me, because in either of them the harm of not doing it the other way is really marginal.

@Bakual
Copy link
Contributor

Bakual commented Sep 19, 2014

Either way will work.
Adding is_dir checks will make the code a tiny bit faster since you only check the presence of the path once instead for each field. However that is probably only measureable if you are talking about some hundred fields. In practice that will be seldom the case. Also, extension which have so many fields, most likely have at least one custom field and thus already have this path added anyway.

On the other hand it makes the code harder to read if you have

if (is_dir(JPATH_COMPONENT . '/models/forms'))
{
    JForm::addFormPath(JPATH_COMPONENT . '/models/forms');
}

if (is_dir(JPATH_COMPONENT . '/models/fields'))
{
    JForm::addFieldPath(JPATH_COMPONENT . '/models/fields'); 
}

if (is_dir(JPATH_COMPONENT . '/models/form'))
{
    JForm::addFormPath(JPATH_COMPONENT . '/model/form'); 
}

if (is_dir(JPATH_COMPONENT . '/models/field'))
{
    JForm::addFieldPath(JPATH_COMPONENT . '/model/field'); 
}

instead of

JForm::addFormPath(JPATH_COMPONENT . '/models/forms'); 
JForm::addFieldPath(JPATH_COMPONENT . '/models/fields'); 
JForm::addFormPath(JPATH_COMPONENT . '/model/form'); 
JForm::addFieldPath(JPATH_COMPONENT . '/model/field'); 

On a sidenote: Performance is important for pages which are shown to visitors on a regular basis. Performance doesn't really matter that much if we're talking about administrator views like we're talking here. It's not like you're going to request a module edit view several times per second.

For me, code readabilty wins hands down in this case. But it may be a personal preference. I don't care that much honestly.

@dgrammatiko
Copy link
Contributor Author

I guess I’ll better leave it as is right now, till we get some more input. And if it gets some attention and successful tests I will also provide the documentation changes needed as well.

@dgrammatiko dgrammatiko changed the title Introduce predefined path in plugins, modules, templates for custom fields in the manifest file Automate the inclusion of fields path for plugins, modules, templates Sep 24, 2014
@b2z
Copy link
Member

b2z commented Sep 24, 2014

Should I apply only this PR to test it?

This comment was created with the J!Tracker Application at http://issues.joomla.org/.

@dgrammatiko
Copy link
Contributor Author

@b2z For the plugin and module it will be easier if you apply also patch #4240 because there is some custom field there so you can use it. For the templates part follow the instructions at #4295.

@jissues-bot jissues-bot changed the title Automate the inclusion of fields path for plugins, modules, templates Introduce predefined path in plugins, modules, templates for custom fields in the manifest file Sep 26, 2014
@dgrammatiko
Copy link
Contributor Author

I will break this to three PRs with some dummy template, module, plugin to make testing easier

@izharaazmi
Copy link
Contributor

@dgt41 @Bakual @sovainfo Why don't we make the is_dir implicit in JFormHelper::addPath() https://github.com/joomla/joomla-cms/blob/staging/libraries/joomla/form/helper.php#L307 and prevent adding of an invalid path in the first place.
Why are we discussing about the repetitive checks, be it anywhere!

@brianteeman
Copy link
Contributor

Sorry replied to wrong post

@dgrammatiko
Copy link
Contributor Author

@izharaazmi you got a point here!

@sovainfo
Copy link
Contributor

@izharaazmi Sounds good to me in this case. Normally not in favor of postponing things but it looks like that is the right place to avoid adding non-existing folders to the path.
That might also be the correct place to translate /administrator. Although I would prefer the xml to be extended with an attribute for application and extending the path in case of administrator. The translation would be required still for dealing with old xml though

This should definitely be applied to J3, finally dealing with the inconsistency of suggesting the folder can be named different from administrator and hardcoding it in the rest of the software.

@@ -555,6 +555,10 @@ public function getForm($data = array(), $loadData = true)
$id = JArrayHelper::getValue($data, 'id');
}

// Add the default fields directory
$baseFolder = ($clientId) ? JPATH_ADMINISTRATOR : JPATH_SITE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use JPATH_BASE here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For modules and templates JPATH_BASE won't work as the com_modules and com_templates (in administrator) need to know if the module or template in question is front end or backend. Using JPATH_BASE will always fall back to administrator

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, I missed that the clientId comes from the data and not the current active client.

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 this pull request may close these issues.

None yet

8 participants