Skip to main content

Render-Element and Form-Element

Render Element

Provides a base class for render element plugins.

Render elements are referenced in render arrays; see the Render API topic for an overview of render arrays and render elements.

The elements of render arrays are divided up into properties (whose keys start with #) and children (whose keys do not start with #).
The properties provide data or settings that are used in rendering.
Some properties are specific to a particular type of render element, some are available for any render element, and some are available for any form input element.
A list of the properties that are available for all render elements follows;
The properties that are for all form elements are documented on FormElement, and properties specific to a particular element are documented on that element's class.
See the Render API topic for a list of the most commonly-used properties.

Many of the properties are strings that are displayed to users.
These strings, if they are literally provided by your module, should be internationalized and translated;
see the Internationalization topic for more information.
Note that although in the properties list that follows, they are designated to be of type string, they would generally end up being \Drupal\Core\StringTranslation\TranslatableMarkup objects instead.

Here is the list of the properties used during the rendering of all render elements:

  • #access: (bool) Whether the element is accessible or not. When FALSE, the element is not rendered and user-submitted values are not taken into consideration.
  • #access_callback: A callable or function name to call to check access. Argument: element.
  • #allowed_tags: (array) Array of allowed HTML tags for XSS filtering of #markup, #prefix, #suffix, etc.
  • #attached: (array) Array of attachments associated with the element. See the "Attaching libraries in render arrays" section of the Render API topic for an overview, and \Drupal\Core\Render\AttachmentsResponseProcessorInterface::processAttachments for a list of what this can contain. Besides this list, it may also contain a 'placeholders' element; see the Placeholders section of the Render API topic for an overview.
  • #attributes: (array) HTML attributes for the element. The first-level keys are the attribute names, such as 'class', and the attributes are usually given as an array of string values to apply to that attribute (the rendering system will concatenate them together into a string in the HTML output).
  • #cache: (array) Cache information. See the Caching section of the Render API topic for more information.
  • #children: (array, internal) Array of child elements of this element. Set and used during the rendering process.
  • #create_placeholder: (bool) TRUE if the element has placeholders that are generated by #lazy_builder callbacks. Set internally during rendering in some cases. See also #attached.
  • #defaults_loaded: (bool) Set to TRUE during rendering when the defaults for the element #type have been added to the element.
  • #value: (mixed) A value that cannot be edited by the user.
  • #has_garbage_value: (bool) Internal only. Set to TRUE to indicate that the #value property of an element should not be used or processed.
  • #id: (string) The HTML ID on the element. This is automatically set for form elements, but not for all render elements; you can override the default value or add an ID by setting this property.
  • #lazy_builder: (array) Array whose first element is a lazy building callback (callable), and whose second is an array of scalar arguments to the callback. To use lazy building, the element array must be very simple: no properties except #lazy_builder, #cache, #weight, and #create_placeholder, and no children. A lazy builder callback typically generates #markup and/or placeholders; see the Placeholders section of the Render API topic for information about placeholders.
  • #markup: (string) During rendering, this will be set to the HTML markup output. It can also be set on input, as a fallback if there is no theming for the element. This will be filtered for XSS problems during rendering; see also #plain_text and #allowed_tags.
  • #plain_text: (string) Elements can set this instead of #markup. All HTML tags will be escaped in this text, and if both #plain_text and #markup are provided, #plain_text is used.
  • #post_render: (array) Array of callables or function names, which are called after the element is rendered. Arguments: rendered element string, children.
  • #pre_render: (array) Array of callables or function names, which are called just before the element is rendered. Argument: $element. Return value: an altered $element.
  • #prefix: (string) Text to render before the entire element output. See also #suffix. If it is not already wrapped in a safe markup object, will be filtered for XSS safety.
  • #printed: (bool, internal) Set to TRUE when an element and its children have been rendered.
  • #render_children: (bool, internal) Set to FALSE by the rendering process if the #theme call should be bypassed (normally, the theme is used to render the children). Set to TRUE by the rendering process if the children should be rendered by rendering each one separately and concatenating.
  • #suffix: (string) Text to render after the entire element output. See also #prefix. If it is not already wrapped in a safe markup object, will be filtered for XSS safety.
  • #theme: (string) Name of the theme hook to use to render the element. A default is generally set for elements; users of the element can override this (typically by adding __suggestion suffixes).
  • #theme_wrappers: (array) Array of theme hooks, which are invoked after the element and children are rendered, and before #post_render functions.
  • #type: (string) The machine name of the type of render/form element.
  • #weight: (float) The sort order for rendering, with lower numbers coming before higher numbers. Default if not provided is zero; elements with the same weight are rendered in the order they appear in the render array.

 

Form Element

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

The elements of form arrays are divided up into properties (whose keys start with #) and children (whose keys do not start with #).
The properties provide data or settings that are used in rendering and form processing.
Some properties are specific to a particular type of form/render element, some are available for any render element, and some are available for any form input element.
A list of the properties that are available for all form elements follows;
see RenderElement for some additional information, as well as a list of properties that are common to all render elements (including form elements).
Properties specific to a particular element are documented in that element's class.

Here is a list of properties that are used during the rendering and form processing of form elements:

  • #after_build: (array) Array of callables or function names, which are called after the element is built. Arguments: $element, $form_state.
  • #ajax: (array) Array of elements to specify Ajax behavior. See the Ajax API topic for more information.
  • #array_parents: (string[], read-only) Array of names of all the element's parents (including itself) in the render array. See also #parents, #tree.
  • #default_value: Default value for the element. See also #value.
  • #description: (string) Help or description text for the element. In an ideal user interface, the #title should be enough to describe the element, so most elements should not have a description; if you do need one, make sure it is translated. If it is not already wrapped in a safe markup object, it will be filtered for XSS safety.
  • #disabled: (bool) If TRUE, the element is shown but does not accept user input.
  • #element_validate: (array) Array of callables or function names, which are called to validate the input. Arguments: $element, $form_state, $form.
  • #field_prefix: (string) Prefix to display before the HTML input element. Should be translated, normally. If it is not already wrapped in a safe markup object, will be filtered for XSS safety. Note that the contents of this prefix are wrapped in a <span> element, so the value should not contain block level HTML. Any HTML added must be valid, i.e. any tags introduced inside this prefix must also be terminated within the prefix.
  • #field_suffix: (string) Suffix to display after the HTML input element. Should be translated, normally. If it is not already wrapped in a safe markup object, will be filtered for XSS safety. Note that the contents of this suffix are wrapped in a <span> element, so the value should not contain block level HTML. Any HTML must also be valid, i.e. any tags introduced inside this suffix must also be terminated within the suffix.
  • #value: (mixed) A value that cannot be edited by the user.
  • #has_garbage_value: (bool) Internal only. Set to TRUE to indicate that the #value property of an element should not be used or processed.
  • #input: (bool, internal) Whether or not the element accepts input.
  • #parents: (string[], read-only) Array of names of the element's parents for purposes of getting values out of $form_state. See also #array_parents, #tree.
  • #process: (array) Array of callables or function names, which are called during form building. Arguments: $element, $form_state, $form.
  • #processed: (bool, internal) Set to TRUE when the element is processed.
  • #required: (bool) Whether or not input is required on the element.
  • #states: (array) Information about JavaScript states, such as when to hide or show the element based on input on other elements. See \Drupal\Core\Form\FormHelper::processStates() for documentation.
  • #title: (string) Title of the form element. Should be translated.
  • #title_display: (string) Where and how to display the #title. Possible values:

     

    • before: Label goes before the element (default for most elements).
    • after: Label goes after the element (default for radio elements).
    • invisible: The label is there but is made invisible using CSS.
    • attribute: Make it the title attribute (hover tooltip).
  • #tree: (bool) TRUE if the values of this element and its children should be hierarchical in $form_state; FALSE if the values should be flat. See also #parents, #array_parents.
  • #value_callback: (callable) Callable or function name, which is called to transform the raw user input to the element's value. Arguments: $element, $input, $form_state.

 

Examples

 

        // form attributes
        $form['#attributes']['class'] = ['sentry-main-form sentry-form drupalvip-form'];           
        $form['#attached']['library'][] = 'drupalvip_sentry/style';