Customizing the form display of a node

New in Drupal 8 is the availability of form display modes. Form modes allow a site administrator to configure different field configurations for each content entity bundle edit form. In the case of nodes, you have the ability to rearrange and alter the display of fields and properties on the node edit form.

In this recipe we'll modify the default form for creating the Article content type that comes with the standard installation profile.

Customizing the form display of a node

How to do it…

  1. To customize the form display mode, visit Structure and then Content Types.
  2. We will modify the Article content type's form. Click on the expand the operations drop down and select Manage form display.
    How to do it…
  3. First we will modify the Comments field. From the Widget dropdown choose the Hidden option to remove it from the form. Follow the same steps for the sticky at top of lists field.
  4. Click on the settings cog for the Body field. Enter in a placeholder for the field, such as Enter your article text here. Click on Update.

    Note

    Note:

    The placeholder will only appear on a textarea using a text format that does not provide a WYSIWYG editor.

  5. Click the Save button at the bottom of the page to save your changes. You have now customized the form display!
  6. Visit Content, Add Content, and then Article. Note that the comment settings are no longer displayed, nor the sticky options under promotion options:
    How to do it…

How it works…

Entities in Drupal have various view modes for each bundle. In Drupal 7 there were only display view modes, which are covered in the next recipe. Drupal 8 brings in new form modes to allow for more control of how an entity edit form is displayed.

Form display modes are configuration entities. Form display modes dictate how the DrupalCoreEntityContentEntityForm class will build a form when an entity is edited. This will always be set to default unless changed or specified specifically to a different mode programmatically.

Since form display modes are configuration entities they can be exported using configuration management.

Hidden field properties will have no value, unless there is a provided default value. For example, if you hide the Authoring information without providing code to set a default value the content will be authored by anonymous (no user).

There's more…

Managing form display modes

Form display modes for all entities are managed under one area and are enabled for each bundle type. You must first create a display mode and then it can be configured through the bundle manage interface.

Programmatically providing a default to hidden form items

In Chapter 6, Creating Forms with the Form API, we will have a recipe that details altering forms. In order to provide a default value for an entity property hidden on the form display, you will need to alter the form and provide a default value. The Field API provides a way to set a default value when fields are created.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset