
This patch introduces two new concepts:
- A renderable text element that will be localized when printed.
- Replaces currently fragmented and unreadable modules help text.
The first part introduces a 'text' element that is a renderable array + a helper function to build such texts, text().
/**
* Build a structured text element to be rendered later.
*
* @param $name
* Unique text name
* @param $text
* HTML text
* @param $variables
* Optional variables for text replacement
*
* @return
* Text array
*/
function text($name, $text, $variables = array(), $options = array()) {
return array(
'#type' => 'text',
'#name' => $name,
'#text' => $text,
'#variables' => $variables,
'#options' => $options,
);
}
Improves the help system to make use of these elements. Done for the node module only as a proof of concept. Some example:
/**
* Implements hook_help().
*/
function node_help($path, $arg) {
....
switch ($path) {
case 'admin/help#node':
$build['about'] = text('node.help.admin.about',
'<h3>About</h3>' .
'<p>The Node module manages the creation, editing, deletion, settings, and display of the ' .
'main site content. Content items managed by the Node module are typically displayed as ' .
'pages on your site, and include a title, some meta-data (author, creation time, content ' .
'type, etc.), and optional fields containing text or other data (fields are managed by the ' .
'<a href="@field">Field module</a>). For more information, see the online handbook entry for ' .
'<a href="@node">Node module</a></p>',
array(
'@node' => 'http://drupal.org/handbook/modules/node',
'@field' => url('admin/help/field'),
)
);
return $build;
}
Pros:
- Text that have a proper identifier (name) can be altered by modules.
- Only text that will be displayed will be localized.
- Localization system though it defaults to current one is pluggable by altering the text element
- Help text for modules is much more readable and flexible.
- It can be used for many other texts in modules that can be 'altered' or 'localized' using different systems.
- It can be also used for configurable texts, localization is pluggable and flexible enough.
Cons:
- Would need improving the po extractor to extract these texts for localization.
- Would require some changes to how big texts are currently translated (though it would make it more powerful and flexible, translators would be able to work with big texts in context, etc..)
Comment | File | Size | Author |
---|---|---|---|
#10 | interdiff.txt | 5.25 KB | alansaviolobo |
#10 | add_renderable-1444980-10.patch | 9.07 KB | alansaviolobo |
#1 | 1444980_text_element.patch | 13.29 KB | jose reyero |
Comments
Comment #1
jose reyero CreditAttribution: jose reyero commentedHere's the patch, tagging the issue.
Comment #2
jose reyero CreditAttribution: jose reyero commentedRelated, this other patch would first simplify help texts, #1445144: Build better tokens with parameters (and use them to replace help text)
Then we may need to add token replacement capabilities to these renderable texts.
Comment #3
andypostGreat idea but not sure about text() function. maybe better to rethink a hook_help() to return $build array
Also we have #markup and #item elements that could be extended to support translation
Edit: patch has a lot of Tab characters
Comment #4
jhodgdonI was asked to comment on this in regards to the help system. Although we had thougts of completely getting rid of the current hook_help() system for Drupal 8, at this point I can confidently say that it's not going to happen for Drupal 8 (not going into the reasons why, but it isn't).
That said, I don't understand from this issue why the text() function is better than t(), but if you think it is, I have no objection to changing hook_help() to use it.
Comment #5
jhodgdonJust to clarify... From the perspective of someone writing hook_help() text, changing from t() to text() doesn't appear to change anything I would need to do, except that I have to think of a unique text name to use as the first argument to text(). The other two arguments, unless I am mistaken, are the same as t().
So can you clarify what the purpose of this change would be? Who is it helping (translators maybe?), and how?
Comment #6
dawehnerIt would be good to document the #localize, #localize_function and #replace feature.
If there is a text() function it should better explain when it's used instead of just plain t().
Here and below in the hook_help implementations are tabs instead of spaces.
One related issue here: #1484002: Add support for named texts for the locale system. Introducing drupal_text()
Comment #7
jose reyero CreditAttribution: jose reyero commentedSince feedback from documentation team is not possitive (I understand they have different longer term plans) but I still think building texts as arrays is a good idea (so it can be altered, localized later if finally printed, etc) I'm going to re-roll this patch with some important changes:
- Keep help texts as they are (I mean use the same strings) but still wrap them in arrays.
- Make names optional, thus text() would be pretty much like t(), but it will still produce an element array that will be localized later only when rendered.
- Add support for formats (check_markup) filters (filter_xss...), etc..
- Add support for tags and attributes (theme_html_tag) since these texts are often wrapped with some more HTML.
So the main idea of this one is that for longer texts we produce renderable arrays and they can be altered before they go through other operations like running them through t() or applying filters or formats. This way if the text is not finally rendered (a module takes it out the render array or changes it) we don't waste time localizing or formatting it.
Comment #8
jair CreditAttribution: jair commentedComment #10
alansaviolobo CreditAttribution: alansaviolobo commentedrerolled the patch. There was a problem when creating the interdiff.
Comment #12
jhodgdonUm.... How is this text going to be translatable? I mean, how is it going to get into the localize.drupal.org databaes?
And I don't really think this is any more readable than the current way of doing things anyway? How is making a text element more readable than calling t()? The other changes in this patch (reformatting) could be done in t() anyway?
See also #5.
Comment #13
andypost8.1 meterial
Comment #14
jhodgdonOh, I forgot about this issue!
So I actually have a working system for Configurable Help topics that creates plugins for text segments, in a sandbox project. I should adapt that patch for Core here. See https://www.drupal.org/sandbox/jhodgdon/2369943
Using that system, a hook_help() implementation like this:
would become a render array, something like this:
Then the 'text_sections' render element and the various text section plugins take care of making this into H3, paragraph, and DL/DT/DD elements.
It doesn't take care of the localization part -- we already have t() for that -- but it does take care of abstracting away some of the embedded HTML stuff.
Thoughts? If this seems like an improvement, I can make that part of the sandbox module into a patch here.
Comment #15
jhodgdonAs a note... One of the problems identified in the current issue summary here is not really a problem any more: t() is now not translating until later. Instead, it makes a "translatable markup" object and it is only translated if the translation is actually being rendered.
I think that the patch proposed earlier on this issue is not in line with how we are currently doing things in Core either... The translation system has been overhauled with these TranslatableMarkup objects, and rather than introducing a function like text() in common.inc, we'd want to use the render system more directly.
Also, the proposal in the issue summary, where the HTML tags would be added back into the translated strings and the strings would become very long, is not viable given localize.drupal.org. See meta issue #2592487: [meta] Help system overhaul for more details... adding this as its child also.
Comment #17
jhodgdonAdding related issue, which also had a patch, but which was abandoned recently.
Comment #24
vacho CreditAttribution: vacho at Skilld commentedI think that rather than make another patch(or re-roll) needs to define a good and concerted solutions
With the current way to write help for a module
Agree:
- It isn't have a localizeable text element for use at another part of code.
- It isn't readable and flexible .
- It isn't pluggable.
Current proposal solution:
- Chunk the current $output variable at $output associative array when you can divide in parts your help and this can be standardized too.
In concept It is too according with @jhodgdon proposal solution but in other way.
So to recover the discussion
Another proposal solution:
I think that this is an use that I really will like when I writing a help of module:
Where we will write only the main thinks on documentations with references to hard documentations that almost all contribute modules have in other places.