formSingleElement example

    $elements = [];

    // If the widget is handling multiple values (e.g Options), or if we are     // displaying an individual element, just get a single form element and make     // it the $delta value.     if ($this->handlesMultipleValues() || isset($get_delta)) {
      $delta = $get_delta ?? 0;
      $element = [
        '#title' => $this->fieldDefinition->getLabel(),
        '#description' => $this->getFilteredDescription(),
      ];
      $element = $this->formSingleElement($items$delta$element$form$form_state);

      if ($element) {
        if (isset($get_delta)) {
          // If we are processing a specific delta value for a field where the           // field module handles multiples, set the delta in the result.           $elements[$delta] = $element;
        }
        else {
          // For fields that handle their own processing, we cannot make           // assumptions about how the field is structured, just merge in the           // returned element.
$description = $this->getFilteredDescription();

    $elements = [];

    $delta = 0;
    // Add an element for every existing item.     foreach ($items as $item) {
      $element = [
        '#title' => $title,
        '#description' => $description,
      ];
      $element = $this->formSingleElement($items$delta$element$form$form_state);

      if ($element) {
        // Input field for the delta (drag-n-drop reordering).         if ($is_multiple) {
          // We name the element '_weight' to avoid clashing with elements           // defined by widget.           $element['_weight'] = [
            '#type' => 'weight',
            '#title' => $this->t('Weight for row @number', ['@number' => $delta + 1]),
            '#title_display' => 'invisible',
            // Note: this 'delta' is the FAPI #type 'weight' element's property.
Home | Imprint | This part of the site doesn't use cookies.