Skip to content

mradcliffe/typed_widget

Repository files navigation

Typed Widget

Typed Widget provides a service to dynamically create form elements based on Typed Data data types.

Build Status

Usage

  1. Get a form element for a primitive data type such as a ISO 8601 date.
$formBuilder = \Drupal::service('typed_widget.element_builder');
$form['date'] = $formBuilder->getElementFor('datetime_iso8601');
  1. UNSTABLE: Get form elements for an entity type or one form element for a property on an entity type.

This functionality is unstable and the return value may change. Currently the typed element builder will look up an entity's form handler and use it if it exists. Otherwise the entity type will be treated as a complex data type.

$form = $formBuilder->getElementFor('entity:user');
unset($form['#process']);
$mailElement = $formBuilder->getElementFor('entity:user', 'mail');
  1. Get only form elements that are only required. By default, all properties that are not read-only or computed will have a form element.
$formBuilder->setNonRequiredProperties(FALSE);
$form = $formBuilder->getElementFor('xero_bank_transaction');
  1. Include read-only properties, which appear as disabled form elements. The nonRequiredProperties property must also be set to TRUE (default).
$formBuilder->setReadOnlyProperties(TRUE);
$form = $formBuilder->getElementFor('xero_user');
  1. Get form element required for field property definitions without the context of an entity or form display.
$form['phone'] = $formBuilder->getElementFor('field_item:telephone');
  1. UNSTABLE: Get form elements for an Article node.

This functionality is unstable and the API may change with regard to the way in which the bundle is set or the return output (see above).

$form = $formBuilder->getElementFor('entity:node', NULL, ['type' => 'article']);
$form = $formBuilder->getElementFor('entity:node', 'field_favorite_color', ['type' => 'article']);

TODO

  • Make a stable and usable API for entity types forms.
    • Option to use complex data or form handler.
    • Option to set required options.
    • Split element builder for entity types into a separate class like primitive element builder.
  • Add an interface that non-entity complex data can use to implement:
    • custom form handler
    • custom view handler
  • Support typed config?
  • Support add more functionality for list elements.

About

Typed Widget Drupal module

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages