/**
  * {@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;
 }
 /**
  * {@inheritdoc}
  */
 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $referenced_entities = $items->referencedEntities();
     $element = parent::formElement($items, $delta, $element, $form, $form_state);
     // If this is an existing (not new item).
     if ($delta < count($referenced_entities)) {
         // Mark element as being existing, not new item.
         // Top level of the returned element must be called 'target_id',
         // so we cannot create a container.
         // Autocomplete element does some fancy processing to handle empty strings,
         // so we must use an autocomplete element not a hidden or textfield element.
         // But #states[#visible] does not seem to have an option to always hide.,
         // and autocomplete elements don't seem to accept #attributes, so we must
         // use #prefix and #suffix to add a class so that we can hide it.
         $element['#prefix'] = '<div class="er-enhanced-existing">';
         $element['#suffix'] = '</div>';
         if ($this->getSetting('preview')) {
             // Add preview.
             $element['#prefix'] = '<div class="er-enhanced-existing er-enhanced-previewing">';
             $element['#attached']['library'][] = 'ahs_er_enhanced/preview';
             $entityTypeName = $referenced_entities[$delta]->getEntityType()->id();
             $view_builder = \Drupal::entityTypeManager()->getViewBuilder($entityTypeName);
             $preview = $view_builder->view($referenced_entities[$delta], $this->getSetting('preview_view_mode'));
             $element['preview_container'] = ['#type' => 'container', '#attributes' => ['class' => ['er-enhanced-preview']], 'preview' => $preview];
             // Add a remove link to the preview.
             $element['remove'] = ['#markup' => '<a class="er-enhanced-remove" href="">' . t('Remove') . '</a>'];
             $element['#attached']['library'][] = 'ahs_er_enhanced/remove';
         }
     } else {
         $element['#prefix'] = '<div class="er-enhanced-new">';
         $element['#suffix'] = '</div>';
     }
     return $element;
 }
Exemplo n.º 3
0
  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $element = parent::formElement($items, $delta, $element, $form, $form_state);
    $field_name = $items->getName();
    $name = $field_name . '[' . $delta . '][target_id]';

    $element['target_id']['#target_type'] = 'view';

    $element['target_id']['#ajax'] = array(
      'callback' => array($this, 'getDisplayIds'),
      'event' => 'viewsreference-select',
      'progress' => array(
        'type' => 'throbber',
        'message' => t('Getting display Ids...'),
      ),
    );


    $default_value = isset($items[$delta]->getValue()['display_id']) ? $items[$delta]->getValue()['display_id'] : '';
    if ($default_value == '') {
      $options = $this->getAllViewsDisplayIds();
    }
    else {
      $options = $this->getViewDisplayIds($items[$delta]->getValue()['target_id']);
    }

    // We build a unique class name from field elements and any parent elements that might exist
    // Which will be used to render the display id options in our ajax function
    $class = !empty($element['target_id']['#field_parents']) ? implode('-',
      $element['target_id']['#field_parents']) . '-' : '';
    $class .= $field_name  . '-' . $delta . '-display-id';

    $element['display_id'] = array(
      '#title' => 'Display Id',
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $default_value,
      '#weight' => 10,
      '#attributes' => array(
        'class' => array(
          $class
        )
      ),
      '#states' => array(
        'visible' => array(
          ':input[name="' . $name . '"]' => array('filled' => TRUE),
        ),
      ),
    );

    $element['argument'] = array(
      '#title' => 'Argument',
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]->getValue()['argument']) ? $items[$delta]->getValue()['argument'] : '',
      '#weight' => 20,
      '#states' => array(
        'visible' => array(
          ':input[name="' . $name . '"]' => array('filled' => TRUE),
        ),
      ),
    );

    $element['#attached']['library'][] = 'viewsreference/viewsreference';

    return $element;
  }
Exemplo n.º 4
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;
  }