You are here

public function FieldWidgetComponent::render in Flexiform 8

Render the component in the form.

Overrides FormComponentInterface::render

File

src/Plugin/FormComponentType/FieldWidgetComponent.php, line 119

Class

FieldWidgetComponent
Component class for field widgets.

Namespace

Drupal\flexiform\Plugin\FormComponentType

Code

public function render(array &$form, FormStateInterface $form_state, RendererInterface $renderer) {
  if ($widget = $this
    ->getRenderer()) {
    if (strpos($this->name, ':')) {
      list($namespace, $field_name) = explode(':', $this->name, 2);

      // This is a form entity element so we need to tweak parents so that
      // form state values are grouped by entity namespace.
      $form['#parents'][] = $namespace;

      // Get the items from the entity manager.
      if ($form_entity = $this
        ->getFormEntityManager()
        ->getEntity($namespace)) {
        $items = $form_entity
          ->get($field_name);
      }
      else {

        // Skip this component if we can't get hold of an entity.
        return;
      }
    }
    else {
      $items = $this
        ->getFormEntityManager()
        ->getEntity('')
        ->get($this->name);
    }
    $items
      ->filterEmptyItems();
    $form[$this->name] = $widget
      ->form($items, $form, $form_state);
    $form[$this->name]['#access'] = $items
      ->access('edit');

    // Assign the correct weight. This duplicates the reordering done in
    // processForm(), but is needed for other forms calling this method
    // directly.
    $form[$this->name]['#weight'] = $this->options['weight'];

    // Associate the cache tags for the field definition & field storage
    // definition.
    $field_definition = $this
      ->getFieldDefinition();
    $renderer
      ->addCacheableDependency($form[$this->name], $field_definition);
    $renderer
      ->addCacheableDependency($form[$this->name], $field_definition
      ->getFieldStorageDefinition());
  }
}