当前位置: 首页>>代码示例>>PHP>>正文


PHP FieldItemListInterface::setValue方法代码示例

本文整理汇总了PHP中Drupal\Core\Field\FieldItemListInterface::setValue方法的典型用法代码示例。如果您正苦于以下问题:PHP FieldItemListInterface::setValue方法的具体用法?PHP FieldItemListInterface::setValue怎么用?PHP FieldItemListInterface::setValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Drupal\Core\Field\FieldItemListInterface的用法示例。


在下文中一共展示了FieldItemListInterface::setValue方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: extractFormValues

 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     if ($this->isDefaultValueWidget($form_state)) {
         $items->filterEmptyItems();
         return;
     }
     $field_name = $this->fieldDefinition->getName();
     $path = array_merge($form['#parents'], array($field_name));
     $submitted_values = $form_state->getValue($path);
     $values = [];
     foreach ($items as $delta => $value) {
         $this->setIefId(sha1($items->getName() . '-ief-single-' . $delta));
         /** @var \Drupal\Core\Entity\EntityInterface $entity */
         if (!($entity = $form_state->get(['inline_entity_form', $this->getIefId(), 'entity']))) {
             return;
         }
         $values[$submitted_values[$delta]['_weight']] = ['entity' => $entity];
     }
     // Sort items base on weights.
     ksort($values);
     $values = array_values($values);
     // Let the widget massage the submitted values.
     $values = $this->massageFormValues($values, $form, $form_state);
     // Assign the values and remove the empty ones.
     $items->setValue($values);
     $items->filterEmptyItems();
     // Put delta mapping in $form_state, so that flagErrors() can use it.
     $field_name = $this->fieldDefinition->getName();
     $field_state = WidgetBase::getWidgetState($form['#parents'], $field_name, $form_state);
     foreach ($items as $delta => $item) {
         $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
         unset($item->_original_delta, $item->_weight);
     }
     WidgetBase::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
 }
开发者ID:atif-shaikh,项目名称:DCX-Profile,代码行数:38,代码来源:InlineEntityFormSimple.php

示例2: formMultipleElements

 /**
  * Overrides \Drupal\Core\Field\WidgetBase::formMultipleElements().
  *
  * Special handling for draggable multiple widgets and 'add more' button.
  */
 protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state)
 {
     $field_name = $this->fieldDefinition->getName();
     $parents = $form['#parents'];
     // 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']);
     }
     // Determine the number of widgets to display.
     $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
     switch ($cardinality) {
         case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED:
             $max = count($items);
             $is_multiple = TRUE;
             break;
         default:
             $max = $cardinality - 1;
             $is_multiple = $cardinality > 1;
             break;
     }
     $title = $this->fieldDefinition->getLabel();
     $description = FieldFilteredMarkup::create($this->fieldDefinition->getDescription());
     $elements = array();
     $delta = 0;
     // Add an element for every existing item.
     foreach ($items as $item) {
         $element = array('#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'] = array('#type' => 'weight', '#title' => t('Weight for row @number', array('@number' => $delta + 1)), '#title_display' => 'invisible', '#delta' => $max, '#default_value' => $item->_weight ?: $delta, '#weight' => 100);
             }
             $elements[$delta] = $element;
             $delta++;
         }
     }
     $empty_single_allowed = $cardinality == 1 && $delta == 0;
     $empty_multiple_allowed = ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || $delta < $cardinality) && !$form_state->isProgrammed();
     // Add one more empty row for new uploads except when this is a programmed
     // multiple form as it is not necessary.
     if ($empty_single_allowed || $empty_multiple_allowed) {
         // Create a new empty item.
         $items->appendItem();
         $element = array('#title' => $title, '#description' => $description);
         $element = $this->formSingleElement($items, $delta, $element, $form, $form_state);
         if ($element) {
             $element['#required'] = $element['#required'] && $delta == 0;
             $elements[$delta] = $element;
         }
     }
     if ($is_multiple) {
         // The group of elements all-together need some extra functionality after
         // building up the full list (like draggable table rows).
         $elements['#file_upload_delta'] = $delta;
         $elements['#type'] = 'details';
         $elements['#open'] = TRUE;
         $elements['#theme'] = 'file_widget_multiple';
         $elements['#theme_wrappers'] = array('details');
         $elements['#process'] = array(array(get_class($this), 'processMultiple'));
         $elements['#title'] = $title;
         $elements['#description'] = $description;
         $elements['#field_name'] = $field_name;
         $elements['#language'] = $items->getLangcode();
         $elements['#display_field'] = (bool) $this->getFieldSetting('display_field');
         // The field settings include defaults for the field type. However, this
         // widget is a base class for other widgets (e.g., ImageWidget) that may
         // act on field types without these expected settings.
         $field_settings = $this->getFieldSettings() + array('display_field' => NULL);
         $elements['#display_field'] = (bool) $field_settings['display_field'];
         // Add some properties that will eventually be added to the file upload
         // field. These are added here so that they may be referenced easily
         // through a hook_form_alter().
         $elements['#file_upload_title'] = t('Add a new file');
         $elements['#file_upload_description'] = array('#theme' => 'file_upload_help', '#description' => '', '#upload_validators' => $elements[0]['#upload_validators'], '#cardinality' => $cardinality);
     }
     return $elements;
 }
开发者ID:vinodpanicker,项目名称:drupal-under-the-hood,代码行数:88,代码来源:FileWidget.php

示例3: extractFormValues

 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     if ($this->isDefaultValueWidget($form_state)) {
         $items->filterEmptyItems();
         return;
     }
     if (!$this->isSubmitRelevant($form, $form_state)) {
         return;
     }
     $field_name = $this->fieldDefinition->getName();
     // Extract the values from $form_state->getValues().
     $parents = array_merge($form['#parents'], [$field_name, 'form']);
     $ief_id = sha1(implode('-', $parents));
     $this->setIefId($ief_id);
     $inline_entity_form_state = $form_state->get('inline_entity_form');
     if (isset($inline_entity_form_state[$this->getIefId()])) {
         $values = $inline_entity_form_state[$this->getIefId()];
         $key_exists = TRUE;
     } else {
         $values = [];
         $key_exists = FALSE;
     }
     if ($key_exists) {
         $values = $values['entities'];
         // Account for drag-and-drop reordering if needed.
         if (!$this->handlesMultipleValues()) {
             // Remove the 'value' of the 'add more' button.
             unset($values['add_more']);
             // The original delta, before drag-and-drop reordering, is needed to
             // route errors to the corect form element.
             foreach ($values as $delta => &$value) {
                 $value['_original_delta'] = $delta;
             }
             usort($values, function ($a, $b) {
                 return SortArray::sortByKeyInt($a, $b, '_weight');
             });
         }
         foreach ($values as $delta => &$item) {
             /** @var \Drupal\Core\Entity\EntityInterface $entity */
             $entity = $item['entity'];
             if (!empty($item['needs_save'])) {
                 $entity->save();
             }
             if (!empty($item['delete'])) {
                 $entity->delete();
                 unset($items[$delta]);
             }
         }
         // Let the widget massage the submitted values.
         $values = $this->massageFormValues($values, $form, $form_state);
         // Assign the values and remove the empty ones.
         $items->setValue($values);
         $items->filterEmptyItems();
         // Put delta mapping in $form_state, so that flagErrors() can use it.
         $field_state = WidgetBase::getWidgetState($form['#parents'], $field_name, $form_state);
         foreach ($items as $delta => $item) {
             $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
             unset($item->_original_delta, $item->_weight);
         }
         WidgetBase::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
     }
 }
开发者ID:tedbow,项目名称:scheduled-updates-demo,代码行数:65,代码来源:InlineEntityFormComplex.php

示例4: extractFormValues

 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     $field_name = $this->fieldDefinition->getName();
     // Extract the values from $form_state->getValues().
     $path = array_merge($form['#parents'], array($field_name));
     $key_exists = NULL;
     $values = NestedArray::getValue($form_state->getValues(), $path, $key_exists);
     if ($key_exists) {
         // Account for drag-and-drop reordering if needed.
         if (!$this->handlesMultipleValues()) {
             // Remove the 'value' of the 'add more' button.
             unset($values['add_more']);
             // The original delta, before drag-and-drop reordering, is needed to
             // route errors to the correct form element.
             foreach ($values as $delta => &$value) {
                 $value['_original_delta'] = $delta;
             }
             usort($values, function ($a, $b) {
                 return SortArray::sortByKeyInt($a, $b, '_weight');
             });
         }
         // Let the widget massage the submitted values.
         $values = $this->massageFormValues($values, $form, $form_state);
         // Assign the values and remove the empty ones.
         $items->setValue($values);
         $items->filterEmptyItems();
         // Put delta mapping in $form_state, so that flagErrors() can use it.
         $field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
         foreach ($items as $delta => $item) {
             $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
             unset($item->_original_delta, $item->_weight);
         }
         static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
     }
 }
开发者ID:ddrozdik,项目名称:dmaps,代码行数:38,代码来源:WidgetBase.php

示例5: extractFormValues

 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     if ($this->isDefaultValueWidget($form_state)) {
         $items->filterEmptyItems();
         return;
     }
     $trigger = $form_state->getTriggeringElement();
     if (empty($trigger['#ief_submit_trigger'])) {
         return;
     }
     $field_name = $this->fieldDefinition->getName();
     $parents = array_merge($form['#parents'], [$field_name, 'form']);
     $ief_id = sha1(implode('-', $parents));
     $this->setIefId($ief_id);
     $inline_entity_form_state = $form_state->get('inline_entity_form');
     if (isset($inline_entity_form_state[$this->getIefId()])) {
         $values = $inline_entity_form_state[$this->getIefId()];
         $key_exists = TRUE;
     } else {
         $values = [];
         $key_exists = FALSE;
     }
     if ($key_exists) {
         // If the inline entity form is still open, then its entity hasn't
         // been transfered to the IEF form state yet.
         if (empty($values['entities']) && !empty($values['form'])) {
             // @todo Do the same for reference forms.
             if ($values['form'] == 'add') {
                 $element = NestedArray::getValue($form, [$field_name, 'widget', 'form']);
                 $entity = $element['inline_entity_form']['#entity'];
                 $values['entities'][] = ['entity' => $entity];
             }
         }
         $values = $values['entities'];
         // Account for drag-and-drop reordering if needed.
         if (!$this->handlesMultipleValues()) {
             // Remove the 'value' of the 'add more' button.
             unset($values['add_more']);
             // The original delta, before drag-and-drop reordering, is needed to
             // route errors to the corect form element.
             foreach ($values as $delta => &$value) {
                 $value['_original_delta'] = $delta;
             }
             usort($values, function ($a, $b) {
                 return SortArray::sortByKeyInt($a, $b, '_weight');
             });
         }
         // Let the widget massage the submitted values.
         $values = $this->massageFormValues($values, $form, $form_state);
         // Assign the values and remove the empty ones.
         $items->setValue($values);
         $items->filterEmptyItems();
         // Put delta mapping in $form_state, so that flagErrors() can use it.
         $field_state = WidgetBase::getWidgetState($form['#parents'], $field_name, $form_state);
         foreach ($items as $delta => $item) {
             $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
             unset($item->_original_delta, $item->_weight);
         }
         WidgetBase::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
     }
 }
开发者ID:CIGIHub,项目名称:bsia-drupal8,代码行数:64,代码来源:InlineEntityFormComplex.php

示例6: formMultipleElements

  /**
   * {@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;
  }
开发者ID:jkyto,项目名称:agolf,代码行数:52,代码来源:EntityqueueDragtableWidget.php

示例7: process_field

 /**
  * Process a field.
  *
  * @param \Drupal\Core\Field\FieldItemListInterface $field
  * @param string $op
  * @param boolean $force if set, we don't check if encryption is enabled, we process the field anyway. This is used during batch processes.
  */
 protected function process_field(\Drupal\Core\Field\FieldItemListInterface $field, $op = 'encrypt', $force = FALSE)
 {
     if (!is_callable([$field, 'getFieldDefinition'])) {
         return;
     }
     /**
      * @var $definition \Drupal\Core\Field\BaseFieldDefinition
      */
     $definition = $field->getFieldDefinition();
     if (!is_callable([$definition, 'get'])) {
         return;
     }
     $field_type = $definition->get('field_type');
     /**
      * Filter out fields that do not have a defined map.
      */
     if (!in_array($field_type, array_keys($this->getFieldEncryptMap()))) {
         return;
     }
     /**
      * @var $storage \Drupal\Core\Field\FieldConfigStorageBase
      */
     $storage = $definition->get('fieldStorage');
     if (is_null($storage)) {
         return;
     }
     /**
      * If we are using the force flag, we always proceed.
      * The force flag is used when we are updating stored fields.
      */
     if (!$force) {
         /**
          * Check if we are updating the field, in that case, skip it now (during
          * the initial entity load.
          */
         if ($this->updatingStoredField === $definition->get('field_name')) {
             return;
         }
         // Check if the field is encrypted.
         $encrypted = $storage->getThirdPartySetting('field_encrypt', 'encrypt', FALSE);
         if (!$encrypted) {
             return;
         }
     }
     /**
      * @var $field \Drupal\Core\Field\FieldItemList
      */
     $field_value = $field->getValue();
     foreach ($field_value as &$value) {
         // Process each of the sub fields that exits.
         $map = $this->fieldEncryptMap[$field_type];
         foreach ($map as $value_name => $service) {
             if (isset($value[$value_name])) {
                 $value[$value_name] = $this->process_value($value[$value_name], $service, $op);
             }
         }
     }
     // Set the new value.
     // We don't need to update the entity because the field setValue does that already.
     $field->setValue($field_value);
 }
开发者ID:nerdstein,项目名称:field_encrypt,代码行数:68,代码来源:FieldEncryptProcessEntities.php

示例8: extractFormValues

 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     $field_name = $this->fieldDefinition->getName();
     // Extract the values from $form_state->getValues().
     $path = array_merge($form['#parents'], array($field_name));
     $key_exists = NULL;
     $values = NestedArray::getValue($form_state->getValues(), $path, $key_exists);
     if ($key_exists) {
         // Account for drag-and-drop reordering if needed.
         if (!$this->handlesMultipleValues()) {
             // Remove the 'value' of the 'add more' button.
             unset($values['add_more']);
             // The original delta, before drag-and-drop reordering, is needed to
             // route errors to the correct form element.
             foreach ($values as $delta => &$value) {
                 $value['_original_delta'] = $delta;
             }
             usort($values, function ($a, $b) {
                 return SortArray::sortByKeyInt($a, $b, '_weight');
             });
         }
         // Let the widget massage the submitted values.
         foreach ($values as $delta => &$value) {
             if (!empty($value['value']) && empty($value['fids'])) {
                 // ready to save the file
                 $provider_manager = \Drupal::service('video.provider_manager');
                 $allowed_providers = $this->getSetting('allowed_providers');
                 $enabled_providers = $provider_manager->loadDefinitionsFromOptionList($allowed_providers);
                 if ($provider_matches = $provider_manager->loadApplicableDefinitionMatches($enabled_providers, $value['value'])) {
                     $definition = $provider_matches['definition'];
                     $matches = $provider_matches['matches'];
                     $uri = $definition['stream_wrapper'] . '://' . $matches['id'];
                     $storage = \Drupal::entityManager()->getStorage('file');
                     $results = $storage->getQuery()->condition('uri', $uri)->execute();
                     if (!(count($results) > 0)) {
                         $user = \Drupal::currentUser();
                         $file = File::Create(['uri' => $uri, 'filemime' => $definition['mimetype'], 'filesize' => 1, 'uid' => $user->id()]);
                         $file->save();
                         unset($values[$delta]);
                         $values[] = array('fids' => array($file->id()), 'data' => serialize($matches));
                     } else {
                         unset($values[$delta]);
                         $values[] = array('fids' => array(reset($results)), 'data' => serialize($matches));
                     }
                 }
             }
         }
         $values = $this->massageFormValues($values, $form, $form_state);
         // Assign the values and remove the empty ones.
         $items->setValue($values);
         $items->filterEmptyItems();
         // Put delta mapping in $form_state, so that flagErrors() can use it.
         $field_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
         foreach ($items as $delta => $item) {
             $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
             unset($item->_original_delta, $item->_weight);
         }
         static::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
     }
 }
开发者ID:kallehauge,项目名称:iamkallehauge.com,代码行数:63,代码来源:VideoEmbedWidget.php

示例9: extractFormValues

 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     if ($this->isDefaultValueWidget($form_state)) {
         $items->filterEmptyItems();
         return;
     }
     $field_name = $this->fieldDefinition->getName();
     $parents = array_merge($form['#parents'], [$field_name]);
     $submitted_values = $form_state->getValue($parents);
     $values = [];
     foreach ($items as $delta => $value) {
         $element = NestedArray::getValue($form, [$field_name, 'widget', $delta]);
         /** @var \Drupal\Core\Entity\EntityInterface $entity */
         $entity = $element['inline_entity_form']['#entity'];
         $weight = isset($submitted_values[$delta]['_weight']) ? $submitted_values[$delta]['_weight'] : 0;
         $values[$weight] = ['entity' => $entity];
     }
     // Sort items base on weights.
     ksort($values);
     $values = array_values($values);
     // Let the widget massage the submitted values.
     $values = $this->massageFormValues($values, $form, $form_state);
     // Assign the values and remove the empty ones.
     $items->setValue($values);
     $items->filterEmptyItems();
     // Populate the IEF form state with $items so that WidgetSubmit can
     // perform the necessary saves.
     $ief_id = sha1(implode('-', $parents));
     $widget_state = ['instance' => $this->fieldDefinition, 'delete' => [], 'entities' => []];
     foreach ($items as $delta => $value) {
         $widget_state['entities'][$delta] = ['entity' => $value->entity, 'needs_save' => TRUE];
     }
     $form_state->set(['inline_entity_form', $ief_id], $widget_state);
     // Put delta mapping in $form_state, so that flagErrors() can use it.
     $field_name = $this->fieldDefinition->getName();
     $field_state = WidgetBase::getWidgetState($form['#parents'], $field_name, $form_state);
     foreach ($items as $delta => $item) {
         $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
         unset($item->_original_delta, $item->_weight);
     }
     WidgetBase::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
 }
开发者ID:CIGIHub,项目名称:bsia-drupal8,代码行数:45,代码来源:InlineEntityFormSimple.php


注:本文中的Drupal\Core\Field\FieldItemListInterface::setValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。