/**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $element = parent::formElement($items, $delta, $element, $form, $form_state);
     $element['target_id']['#tags'] = TRUE;
     $element['target_id']['#default_value'] = $items->referencedEntities();
     return $element;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
   return parent::massageFormValues($values, $form, $form_state);
 }
Exemplo n.º 3
0
  /**
   * {@inheritdoc}
   */
  protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
    $field_name = $this->fieldDefinition->getName();
    $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
    $parents = $form['#parents'];

    // Assign a unique identifier to each widget.
    $id_prefix = implode('-', array_merge($parents, [$field_name]));
    $wrapper_id = Html::getUniqueId($id_prefix . '-add-more-wrapper');
    $this->setWrapperId($wrapper_id);

    // Load the items for form rebuilds from the field state as they might not
    // be in $form_state->getValues() because of validation limitations. Also,
    // they are only passed in as $items when editing existing entities.
    $field_state = static::getWidgetState($parents, $field_name, $form_state);
    if (isset($field_state['items'])) {
      $items->setValue($field_state['items']);
    }

    // Lower the 'items_count' field state property in order to prevent the
    // parent implementation to append an extra empty item.
    if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
      $field_state['items_count'] = (count($items) > 1) ? count($items) - 1 : 0;
      static::setWidgetState($parents, $field_name, $form_state, $field_state);
    }

    $elements = parent::formMultipleElements($items, $form, $form_state);

    if ($elements) {
      if (isset($elements['add_more'])) {
        // Update the HTML wrapper ID with the one generated by us.
        $elements['#prefix'] = '<div id="' . $this->getWrapperId() . '">';

        $add_more_button = $elements['add_more'];
        $add_more_button['#value'] = $this->t('Add item');
        $add_more_button['#ajax']['callback'] = [get_class($this), 'getWidgetElementAjax'];
        $add_more_button['#ajax']['wrapper'] = $this->getWrapperId();

        $elements['add_more'] = [
          '#type' => 'container',
          '#tree' => TRUE,
          '#attributes' => ['class' => ['form--inline']],
          'new_item' => parent::formElement($items, -1, [], $form, $form_state),
          'submit' => $add_more_button,
        ];
      }
    }

    return $elements;
  }
 /**
  * {@inheritdoc}
  */
 public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL)
 {
     $elements = parent::form($items, $form, $form_state);
     // Add class to indicate preview is active.
     if ($this->getSetting('preview')) {
         $elements['#attributes']['class'][] = 'er-enhanced-previewing';
         // Add an Edit link if UI is hidden.
         if ($this->getSetting('preview_hide_ui')) {
             /*
             $elements['er-enhanced-edit'] = [
               '#markup' => '<a class="er-enhanced-edit" style="border:1px solid red;display:none;" href="">' . t('Edit') . '</a>',
               '#visible' => FALSE,
             ];
             */
             if (count($items) < 2 && !empty($this->getSetting('preview_empty_message'))) {
                 $elements['er-enhanced-empty-message'] = ['#markup' => '<p class="er-enhanced-empty-message">' . $this->getSetting('preview_empty_message') . '</p>'];
             }
             $elements['#attributes']['class'][] = 'er-enhanced-hideui-requested';
             $elements['#attached']['library'][] = 'ahs_er_enhanced/hideui';
             //$elements['#attached']['drupalSettings']['ahs_er_enhanced']['hideui']['preview_empty_message'] = $this->getSetting('preview_empty_message');
         }
     }
     return $elements;
 }