예제 #1
0
 /**
  * Gets the URI without the 'internal:' or 'entity:' scheme.
  *
  * The following two forms of URIs are transformed:
  * - 'entity:' URIs: to entity autocomplete ("label (entity id)") strings;
  * - 'internal:' URIs: the scheme is stripped.
  *
  * This method is the inverse of ::getUserEnteredStringAsUri().
  *
  * @param string $uri
  *   The URI to get the displayable string for.
  *
  * @return string
  *
  * @see static::getUserEnteredStringAsUri()
  */
 protected static function getUriAsDisplayableString($uri)
 {
     $scheme = parse_url($uri, PHP_URL_SCHEME);
     // By default, the displayable string is the URI.
     $displayable_string = $uri;
     // A different displayable string may be chosen in case of the 'internal:'
     // or 'entity:' built-in schemes.
     if ($scheme === 'internal') {
         $uri_reference = explode(':', $uri, 2)[1];
         // @todo '<front>' is valid input for BC reasons, may be removed by
         //   https://www.drupal.org/node/2421941
         $path = parse_url($uri, PHP_URL_PATH);
         if ($path === '/') {
             $uri_reference = '<front>' . substr($uri_reference, 1);
         }
         $displayable_string = $uri_reference;
     } elseif ($scheme === 'entity') {
         list($entity_type, $entity_id) = explode('/', substr($uri, 7), 2);
         // Show the 'entity:' URI as the entity autocomplete would.
         $entity_manager = \Drupal::entityManager();
         if ($entity_manager->getDefinition($entity_type, FALSE) && ($entity = \Drupal::entityManager()->getStorage($entity_type)->load($entity_id))) {
             $displayable_string = EntityAutocomplete::getEntityLabels(array($entity));
         }
     }
     return $displayable_string;
 }
예제 #2
0
파일: Name.php 프로젝트: 318io/318-io
 protected function valueForm(&$form, FormStateInterface $form_state)
 {
     $users = $this->value ? User::loadMultiple($this->value) : array();
     $default_value = EntityAutocomplete::getEntityLabels($users);
     $form['value'] = array('#type' => 'entity_autocomplete', '#title' => $this->t('Usernames'), '#description' => $this->t('Enter a comma separated list of user names.'), '#target_type' => 'user', '#tags' => TRUE, '#default_value' => $default_value, '#process_default_value' => FALSE);
     $user_input = $form_state->getUserInput();
     if ($form_state->get('exposed') && !isset($user_input[$this->options['expose']['identifier']])) {
         $user_input[$this->options['expose']['identifier']] = $default_value;
         $form_state->setUserInput($user_input);
     }
 }
예제 #3
0
  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $field_name = $this->fieldDefinition->getName();
    $parents = $form['#parents'];
    $referenced_entities = $items->referencedEntities();

    if (isset($referenced_entities[$delta])) {
      $entity_label = EntityAutocomplete::getEntityLabels([$referenced_entities[$delta]]);
      $id_prefix = implode('-', array_merge($parents, [$field_name, $delta]));

      $element += [
        '#type' => 'container',
        '#attributes' => ['class' => ['form--inline']],
        'target_id' => [
          '#type' => 'item',
          '#markup' => $entity_label,
          '#default_value' => !$referenced_entities[$delta]->isNew() ? $referenced_entities[$delta]->id() : NULL,
        ],
        'entity' => [
          '#type' => 'value',
          '#default_value' => $referenced_entities[$delta],
        ],
        'remove' => [
          '#type' => 'submit',
          '#name' => strtr($id_prefix, '-', '_') . '_remove',
          '#value' => t('Remove'),
          '#attributes' => ['class' => ['remove-item-submit', 'align-right']],
          '#submit' => [[get_class($this), 'removeSubmit']],
          '#ajax' => [
            'callback' => [get_class($this), 'getWidgetElementAjax'],
            'wrapper' => $this->getWrapperId(),
            'effect' => 'fade',
          ],
        ],
      ];
    }

    return $element;
  }
 /**
  * Returns an entity label in the format needed by the EntityAutocomplete
  * element.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   A Drupal entity.
  *
  * @return string
  *   A string that can be used as a value for EntityAutocomplete elements.
  */
 protected function getAutocompleteInput(EntityInterface $entity)
 {
     return EntityAutocomplete::getEntityLabels(array($entity));
 }
예제 #5
0
 protected function valueForm(&$form, FormStateInterface $form_state)
 {
     $vocabulary = $this->vocabularyStorage->load($this->options['vid']);
     if (empty($vocabulary) && $this->options['limit']) {
         $form['markup'] = array('#markup' => '<div class="form-item">' . $this->t('An invalid vocabulary is selected. Please change it in the options.') . '</div>');
         return;
     }
     if ($this->options['type'] == 'textfield') {
         $terms = $this->value ? Term::loadMultiple($this->value) : array();
         $form['value'] = array('#title' => $this->options['limit'] ? $this->t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->label())) : $this->t('Select terms'), '#type' => 'textfield', '#default_value' => EntityAutocomplete::getEntityLabels($terms));
         if ($this->options['limit']) {
             $form['value']['#type'] = 'entity_autocomplete';
             $form['value']['#target_type'] = 'taxonomy_term';
             $form['value']['#selection_settings']['target_bundles'] = array($vocabulary->id());
             $form['value']['#tags'] = TRUE;
             $form['value']['#process_default_value'] = FALSE;
         }
     } else {
         if (!empty($this->options['hierarchy']) && $this->options['limit']) {
             $tree = $this->termStorage->loadTree($vocabulary->id(), 0, NULL, TRUE);
             $options = array();
             if ($tree) {
                 foreach ($tree as $term) {
                     $choice = new \stdClass();
                     $choice->option = array($term->id() => str_repeat('-', $term->depth) . SafeMarkup::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term)->label()));
                     $options[] = $choice;
                 }
             }
         } else {
             $options = array();
             $query = \Drupal::entityQuery('taxonomy_term')->sort('weight')->sort('name')->addTag('term_access');
             if ($this->options['limit']) {
                 $query->condition('vid', $vocabulary->id());
             }
             $terms = Term::loadMultiple($query->execute());
             foreach ($terms as $term) {
                 $options[$term->id()] = SafeMarkup::checkPlain(\Drupal::entityManager()->getTranslationFromContext($term)->label());
             }
         }
         $default_value = (array) $this->value;
         if ($exposed = $form_state->get('exposed')) {
             $identifier = $this->options['expose']['identifier'];
             if (!empty($this->options['expose']['reduce'])) {
                 $options = $this->reduceValueOptions($options);
                 if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
                     $default_value = array();
                 }
             }
             if (empty($this->options['expose']['multiple'])) {
                 if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
                     $default_value = 'All';
                 } elseif (empty($default_value)) {
                     $keys = array_keys($options);
                     $default_value = array_shift($keys);
                 } elseif ($default_value == array('')) {
                     $default_value = 'All';
                 } else {
                     $copy = $default_value;
                     $default_value = array_shift($copy);
                 }
             }
         }
         $form['value'] = array('#type' => 'select', '#title' => $this->options['limit'] ? $this->t('Select terms from vocabulary @voc', array('@voc' => $vocabulary->label())) : $this->t('Select terms'), '#multiple' => TRUE, '#options' => $options, '#size' => min(9, count($options)), '#default_value' => $default_value);
         $user_input = $form_state->getUserInput();
         if ($exposed && isset($identifier) && !isset($user_input[$identifier])) {
             $user_input[$identifier] = $default_value;
             $form_state->setUserInput($user_input);
         }
     }
     if (!$form_state->get('exposed')) {
         // Retain the helper option
         $this->helper->buildOptionsForm($form, $form_state);
     }
 }