Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I think it would be useful if the menu item path was a dedicated form element registered with hook_element_info().
This would allow to implement any autocomplete, validation, and even the placeholder replacement, as a feature of this form element, not caring about the rest of the form.
The element would behave mostly like a textfield, but it would have a setting $element['#mlid']
, that will be used for the placeholder and autocomplete stuff.
Comments
Comment #1
donquixote CreditAttribution: donquixote commentedOne challenge: For new items, the mlid can only be set once the element itself has been created.
Comment #2
donquixote CreditAttribution: donquixote commentedJust a note: I tried this myself locally, but not really happy with it yet.
At some point we need to run a process function, and this will turn it into a textfield with added validation etc.
But, we can not simply just say $element['#type'] = 'textfield', but rather add an inner element which then is the text field.
$element['textfield'] = array('#type' => 'textfield', '#default_value' => $element['#default_value'], ..)
This sounds to me like too much nesting (esp. if we squeeze yet another step in between, as below).
I also thought about putting in between another element type, that would be called 'linkfield'. So the menu item path field would be turned into a linkfield, this would be turned into a textfield.
Btw, this is a missing feature in Drupal core+contrib, having a dedicated form element for url paths! Even the 'link' module form element only works in combination with field api, not as standalone.
I can commit my stuff to a feature branch, if you want.
Comment #3
donquixote CreditAttribution: donquixote commentedBtw, the first use case I was thinking about would be
#1689436: Allow to edit the menu item path directly from placeholder page.
This is also easier for development, because dpm() will not generate dozens of messages :)
Comment #4
dsdeiz CreditAttribution: dsdeiz commentedWould definitely love that. Thanks!
Comment #5
donquixote CreditAttribution: donquixote commentedPushed to 7.x-1.x-hook_element_info. No promises. Good luck with it :)
Comment #6
dsdeiz CreditAttribution: dsdeiz commentedHi, sorry, didn't have time to tackle this much. Anyway, I'm a little confused.
menu_editor_item_path
would simply be holdingmenu_editor_link_path
? I'm guessing it would contain themenu_editor_link_path
element and a property formlid
whilemenu_editor_link_path
would contain the textfield and include validation? If this is the case, I'm not sure why there need to exist two elements though. Perhaps they can be combined instead.Comment #7
donquixote CreditAttribution: donquixote commentedHi,
I have no strong opinion about this. In the end we might decide that this issue is not useful at all :)
The idea with menu_editor_link_path was to have a form element dedicated for links, with all the validation etc.
In theory, this could be reused in other places where a user can specify a link. E.g. to specify the site frontpage, etc.
However, this would only make sense if the element was provided by Drupal core, or by the "link" module. Unfortunately, the "link" module form element only works within field API.
So, as long as this is all within menu_editor, the additional level probably does not add much benefit.
Also I am not sure if the "custom form element type" is really a useful solution, or if it is better to just create a textfield, and add process and validation callbacks - so the reusable thing would be not the form element type, but the process callback.