Problem/Motivation

We need to document the properties of Form and Render elements that are common to all of them.

Proposed resolution

1. Document the properties that are common to all render elements on the RenderElement class doc header.
2. Document the additional properties that are common to all form elements on the FormElement class doc header.

The documentation can be taken from the current Form API Reference document on api.drupal.org

Also if there are some properties that are common to most form/render elements, they can be documented there now, probably in separate lists from the ones that are all common.

Remaining tasks

Make a patch.

User interface changes

No. API docs only.

API changes

No. API docs only.

Data model changes

No. API docs only.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon created an issue. See original summary.

jhodgdon’s picture

Status: Active » Needs review
FileSize
6.34 KB

Here's a first pass... probably accurate but needs to be reviewed for sure!

metzlerd’s picture

Status: Needs review » Needs work

This looks really good! I learned a lot just from reading it ;)

It took me a moment to get used to the conventions what "read only" actually meant. Not sure if we should tackle that in the render api page.

+++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
@@ -17,6 +17,50 @@
+ * - #attached: (array) Array of attachments associated with the element. Can
+ *   include elements:
+ *   - library: (string[]) Names of CSS/JavaScript libraries to attach.
+ *   - drupalSettings: (array) Drupal JavaScript settings to attach.

This really needs a see reference, but I'm not sure where to. A newbie is likely to just start trying to list css/js files in this list. Have any ideas? Loading css/jss is a primary task for a module developer. I'm kind of surprised we don't have a "Rendering" or "Libraries" section in our core topics list, but perhaps a link to https://www.drupal.org/developing/api/8/assets would be appropriate here?


+++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
@@ -17,6 +17,50 @@
+ * - #id: (string, normally read-only) The HTML ID on the element.

What does (normally) mean here? How would I know whether to set this value or not?

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
5.31 KB
8.13 KB

Thanks for the review!

Actually we do have docs about attachments and I meant to put a @link in the patch but forgot to take care of that in the second pass. Doh!

And regarding ID, I will clarify that. I think we should say something like that this is normally set for you on form elements (I don't think render element rendering normally puts in an ID at all?), but you can override it if necessary. But agreed "normally read-only" isn't very clear. ;)

And @webchick took a quick look at this late last night at BADCamp, and suggested we should also point people to a list of the common properties rather than overwhelming them with a huge list, so I added some additional docs and pointers to our existing docs for that.

See what you think?

metzlerd’s picture

Status: Needs review » Reviewed & tested by the community

That fixes it from my perspective. I'm going to try and test the #id attribute on render elements... I think that's a better pattern for ajax container examples, and should probably update the sample code accordingly.

jhodgdon’s picture

Status: Reviewed & tested by the community » Needs review

Thanks! Probably should have @Xano or someone else like that look this over before it goes in, so leaving Needs Review for now. Also ... I think we may need to verify that all the function args are in the right order for the preprocess, prerender etc. calls.

jhodgdon’s picture

I took a careful look at all the callables things. There was one error: #after_build turns out to be just for forms, and the arguments are just $element, $form_state. So, fixed that. (I looked at where, in FormBuilder, FormValidator, and Renderer, these functions were actually called with call_user_func_array() to verify what was passed in.) I'm confident they're all right now. Also I asked @timplunkett to review so... that should be done shortly.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

This looks great, thanks!

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
@@ -17,6 +17,67 @@
  * Provides a base class for render element plugins.
...
+ * Render elements have properties. Some are specific to a particular type

so this is a super tricky because what are render elements? In Drupal 8 they are really still arrays! So they don't really have properties and it is a bit confusing to say they do in the context of saying this on the base class for render element plugins - which would intimate that render elements are objects which have properties.

I think we are missing #markup, #plain_text and #allowed_tags. Every element will at least have #markup set by rendering and some elements are designed have it set first.

What also might be able to be moved here is the security documentation for #suffix and #prefix.

I also think we need to mention security here since we're saying that many of the properties are displayed to users.

Also I'm pondering where #field_prefix and #field_suffix should go too.

heykarthikwithu’s picture

added #markup, #plain_text and #allowed_tags.

heykarthikwithu’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 10: default-properties-2599442-10.patch, failed testing.

Himanshu5050’s picture

modified patch #10 and resubmitted for review.

heykarthikwithu’s picture

Status: Needs work » Needs review

The last submitted patch, 10: default-properties-2599442-10.patch, failed testing.

jhodgdon’s picture

So... while I appreciate both of your efforts, this issue was assigned to me, and generally if an issue is assigned to someone, that means that they intend to keep working on it. It's polite to at least ask before taking it over; also if you do take it over, you should either unassign it or assign it to yourself. Thanks!

Also if you make a new patch for an existing issue, especially if the patch is fairly long like this one, you really need to make an interdiff file so that reviewers can see what you changed. See
https://www.drupal.org/documentation/git/interdiff

So looking at the differences between my last patch and the latest patch:

#markup, #plain_text, and #allowed_tags were added to the Form class. These should have gone on RenderElement though, and there should still have been a blank line after this documentation before the @see, which was removed in the latest patch.

And Alex's other review comments were not addressed.

So. I am taking this issue back over.... Please let me take care of it until I un-assign it. Thanks! I'll post a separate comment addressing the review comments.

jhodgdon’s picture

RE #9 - we have always called the #-starting elements in render/form arrays "properties", and we have docs explaining this; I will clarify though because you're right, what I wrote wasn't clear.

Also I've added in markup, plain_text, and allowed_tags (in the correct place, and clarified). And added in security information about the prefix/suffix stuff. And I went through the Renderer class and found some more things to add.

One thing we didn't have documented was placeholders. I decided this deserved a new section of the Render API topic, because it was too complicated to describe in a few words.

Anyway... here is a new patch, and an interdiff to the patch in #7.

eojthebrave’s picture

Status: Needs review » Needs work

This is looking good. Thanks Jennifer. I've got a few comments that I think will help to make this a little easier to read, and a few mistakes that should get cleared up before this get committed.

  1. +++ b/core/lib/Drupal/Core/Render/Element/FormElement.php
    @@ -15,18 +15,24 @@
    + * arrays and render elements, and the @link form_api Form API topic @endlink
    

    It looks like the link to https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21... was removed, and I think it should still be here. Specifically, "arrays and render elements" should be a link.

  2. +++ b/core/lib/Drupal/Core/Render/Element/FormElement.php
    @@ -15,18 +15,24 @@
    + * form processing of most/all form elements:
    

    Is it most, or is it all? It seems weird that this is kind of ambiguous. Or is that ambiguity intentional?

  3. +++ b/core/lib/Drupal/Core/Render/Element/FormElement.php
    @@ -35,15 +41,18 @@
    - *   sparingly; make sure it is translated.
    + *   sparingly; make sure it is translated. If it is not already wrapped in
    

    I find the use of the word sparingly a little weird here. Probably because I don't fully understand why we're telling someone to use this property sparingly? Is it because we want you to keep it short and concise, or because there is another better way to provide inline help to users of a form?

  4. +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
    @@ -17,43 +17,77 @@
    + * that although in the properies list they that follows, they are designated
    

    "list they that follows" should be just be "list that follows" I think.

  5. +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
    @@ -17,43 +17,77 @@
    + * - #allowed_tags: (array) Array of allowed HTML tags for XSS filtering of
    + *   #markup, #prefix, #suffix, etc.
    

    I wonder if it makes sense to @see \Drupal\Core\Render\Renderer::ensureMarkupIsSafe here?

  6. +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
    @@ -17,43 +17,77 @@
    + *   theming for the element. This will be filtered during rendering; see also
    + *   #plain_text and #allow_tags.
    

    I think this should be #allowed_tags, not #allow_tags

  7. +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
    @@ -17,43 +17,77 @@
    + * - #plain_text: (string) A plain text override to #markup. HTML tags will
    + *   be escaped in this text.
    

    I was having a little bit of trouble trying to figure out when I should use #markup and when I should use #plain_text, the documentation for \Drupal\Core\Render\Renderer::ensureMarkupIsSafe helped to clear this up a little bit for me but I think we could be a bit clearer about it here. That documentation says, "Render arrays can escape text instead of XSS filtering by setting the #plain_text property instead of #markup." if that's helpful at all.

  8. +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
    @@ -61,11 +95,17 @@
    + * - #render_children: (bool, internal) Set to FALSE by the rendering process
    + *   #theme should be bypassed (it's normally used to render the children).
    

    I think this sentence is missing an "if" somewhere. It doesn't read smoothly without it.

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
13.26 KB
4.21 KB

Thanks very much for the careful review! I think I've addressed everything, except:

item 1 - The entire paragraph here is:

 * Form elements are a subset of render elements, representing elements for
 * HTML forms, which can be referenced in form arrays. See the
 * @link theme_render Render API topic @endlink for an overview of render
 * arrays and render elements, and the @link form_api Form API topic @endlink
 * for an overview of forms and form arrays.
 

So it does still have a link to the theme_render topic? Maybe you were looking at the interdiff and not the actual patch?

item 5 - I didn't want to point people to the Renderer class... that is pretty low-level.

eojthebrave’s picture

Status: Needs review » Reviewed & tested by the community

The cleanup looks good to me. The sections on #plain_text especially are much easier to follow now. I think we can go ahead and this added. Thanks!

jhodgdon’s picture

Related docs issue #2603818: Add defgroups for listing page headers for api.drupal.org is another piece of the "replace the Form API reference" puzzle. Needs review please. :)

alexpott’s picture

Title: Document common form/render element properties » Document common form/render element properties
Status: Reviewed & tested by the community » Fixed

Committed d648b71 and pushed to 8.0.x. Thanks!

  • alexpott committed d648b71 on
    Issue #2599442 by jhodgdon, heykarthikwithu, Himanshu5050, eojthebrave,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

TR’s picture

Status: Closed (fixed) » Active

One thing we didn't have documented was placeholders. I decided this deserved a new section of the Render API topic, because it was too complicated to describe in a few words.

I don't see where #placeholder is documented on RenderElement, FormElement, or any of their subclasses. Yet it is used throughout core. (This is also true of dozens of other tags, BTW.) #2226275: Update Drupal 8 Form API reference was closed as a duplicate of this issue, so it was expected to be fixed here. We have gone from an outdated and hard to maintain "Form API reference" document to no documentation at all for many of the properties.

The Render API overview at https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.... mentions placeholders only in the context of build arrays, not form elements, and mentions that you can use '#attached' => ['placeholders' => ['@foo' => 'replacement']], in a build array, but I don't see that feature used in core and it's not clear whether this is the new way to do things (and core hasn't been updated yet) or its the old way to do things (and the documentation hasn't been update yet) or whether it's one of those dead-ends from the D8 development process that's no longer true. Regardless, #placeholder *does* work, and is used in core, but is not documented.

jhodgdon’s picture

Status: Active » Closed (fixed)

Please, when an issue has been closed for a long time, open a new issue rather than reopening an issue that was already patched. Thanks!

TR’s picture

Well it's been less than four months, not really a long time as far as core issues go. All the context is in this thread, and other threads which have raised this issue have been closed as duplicate of this thread. So I still think it's reasonable to deal with the problem in the same place where it was worked on and should have been resolved.

But if you want, I'll open some new issues for the things that were missed here.

EDIT:

I'm working on this ...
Would you like one issue for everything, say "Document common form/render element properties, Part II" or would you like one issue for each undocumented property, or one issue for each of the elements that are missing property documentation? I'm already up to 30 undocumented properties, and I'm only part way through the list of form and render elements ...

jhodgdon’s picture

Why not start with one issue, and we can make it a Meta if we need to split it up into multiple patches. Thanks!