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


PHP FieldItemListInterface::appendItem方法代码示例

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


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

示例1: formMultipleElements

 /**
  * Special handling to create form elements for multiple values.
  *
  * Handles generic features for multiple fields:
  * - number of widgets
  * - AHAH-'add more' button
  * - table display and drag-n-drop value reordering
  */
 protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state)
 {
     $field_name = $this->fieldDefinition->getName();
     $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
     $parents = $form['#parents'];
     // Determine the number of widgets to display.
     switch ($cardinality) {
         case FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED:
             $field_state = static::getWidgetState($parents, $field_name, $form_state);
             $max = $field_state['items_count'];
             $is_multiple = TRUE;
             break;
         default:
             $max = $cardinality - 1;
             $is_multiple = $cardinality > 1;
             break;
     }
     $title = $this->fieldDefinition->getLabel();
     $description = FieldFilteredMarkup::create(\Drupal::token()->replace($this->fieldDefinition->getDescription()));
     $elements = array();
     for ($delta = 0; $delta <= $max; $delta++) {
         // Add a new empty item if it doesn't exist yet at this delta.
         if (!isset($items[$delta])) {
             $items->appendItem();
         }
         // For multiple fields, title and description are handled by the wrapping
         // table.
         if ($is_multiple) {
             $element = ['#title' => $this->t('@title (value @number)', ['@title' => $title, '@number' => $delta + 1]), '#title_display' => 'invisible', '#description' => ''];
         } else {
             $element = ['#title' => $title, '#title_display' => 'before', '#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' => $this->t('Weight for row @number', array('@number' => $delta + 1)), '#title_display' => 'invisible', '#delta' => $max, '#default_value' => $items[$delta]->_weight ?: $delta, '#weight' => 100);
             }
             $elements[$delta] = $element;
         }
     }
     if ($elements) {
         $elements += array('#theme' => 'field_multiple_value_form', '#field_name' => $field_name, '#cardinality' => $cardinality, '#cardinality_multiple' => $this->fieldDefinition->getFieldStorageDefinition()->isMultiple(), '#required' => $this->fieldDefinition->isRequired(), '#title' => $title, '#description' => $description, '#max_delta' => $max);
         // Add 'add more' button, if not working with a programmed form.
         if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && !$form_state->isProgrammed()) {
             $id_prefix = implode('-', array_merge($parents, array($field_name)));
             $wrapper_id = Html::getUniqueId($id_prefix . '-add-more-wrapper');
             $elements['#prefix'] = '<div id="' . $wrapper_id . '">';
             $elements['#suffix'] = '</div>';
             $elements['add_more'] = array('#type' => 'submit', '#name' => strtr($id_prefix, '-', '_') . '_add_more', '#value' => t('Add another item'), '#attributes' => array('class' => array('field-add-more-submit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name))), '#submit' => array(array(get_class($this), 'addMoreSubmit')), '#ajax' => array('callback' => array(get_class($this), 'addMoreAjax'), 'wrapper' => $wrapper_id, 'effect' => 'fade'));
         }
     }
     return $elements;
 }
开发者ID:ddrozdik,项目名称:dmaps,代码行数:64,代码来源:WidgetBase.php

示例2: formMultipleElements

 public function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state)
 {
     $field_name = $this->fieldDefinition->getName();
     $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
     $parents = $form['#parents'];
     $field_state = static::getWidgetState($parents, $field_name, $form_state);
     $max = $field_state['items_count'];
     $real_item_count = $max;
     $is_multiple = $this->fieldDefinition->getFieldStorageDefinition()->isMultiple();
     $title = $this->fieldDefinition->getLabel();
     $description = FieldFilteredMarkup::create(\Drupal::token()->replace($this->fieldDefinition->getDescription()));
     $elements = array();
     $id_prefix = implode('-', array_merge($parents, array($field_name)));
     $wrapper_id = Html::getUniqueId($id_prefix . '-add-more-wrapper');
     $elements['#prefix'] = '<div id="' . $wrapper_id . '">';
     $elements['#suffix'] = '</div>';
     $field_state['ajax_wrapper_id'] = $wrapper_id;
     static::setWidgetState($parents, $field_name, $form_state, $field_state);
     if ($max > 0) {
         for ($delta = 0; $delta < $max; $delta++) {
             // Add a new empty item if it doesn't exist yet at this delta.
             if (!isset($items[$delta])) {
                 $items->appendItem();
             }
             // For multiple fields, title and description are handled by the wrapping
             // table.
             $element = array('#title' => $is_multiple ? '' : $title, '#description' => $is_multiple ? '' : $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' => $items[$delta]->_weight ?: $delta, '#weight' => 100);
                 }
                 if (isset($element['#access']) && !$element['#access']) {
                     $real_item_count--;
                 } else {
                     $elements[$delta] = $element;
                 }
             }
         }
     }
     $field_state = static::getWidgetState($parents, $field_name, $form_state);
     $field_state['real_item_count'] = $real_item_count;
     static::setWidgetState($parents, $field_name, $form_state, $field_state);
     $entity_manager = \Drupal::entityManager();
     $target_type = $this->getFieldSetting('target_type');
     $bundles = $this->getAllowedTypes();
     $access_control_handler = $entity_manager->getAccessControlHandler($target_type);
     $options = array();
     $access_options = array();
     $dragdrop_settings = $this->getSelectionHandlerSetting('target_bundles_drag_drop');
     foreach ($bundles as $machine_name => $bundle) {
         if ($dragdrop_settings || (!count($this->getSelectionHandlerSetting('target_bundles')) || in_array($machine_name, $this->getSelectionHandlerSetting('target_bundles')))) {
             $options[$machine_name] = $bundle['label'];
             if ($access_control_handler->createAccess($machine_name)) {
                 $access_options[$machine_name] = $bundle['label'];
             }
         }
     }
     if ($real_item_count > 0) {
         $elements += array('#theme' => 'field_multiple_value_form', '#field_name' => $field_name, '#cardinality' => $cardinality, '#cardinality_multiple' => $is_multiple, '#required' => $this->fieldDefinition->isRequired(), '#title' => $title, '#description' => $description, '#max_delta' => $max - 1);
     } else {
         // @todo: properize this.
         $add_text = 'No @title_multiple have been added yet. Select a @title type and press the button below to add one.';
         $element_text = '<label>' . $title . "</label>";
         $element_text .= '<p><em>' . t($add_text, array('@title_multiple' => $this->getSetting('title_plural'), '@title' => $this->getSetting('title'))) . '</em></p>';
         $element_text .= $description ? '<div class="description">' . $description . '</div>' : '';
         $elements += array('#type' => 'container', '#theme_wrappers' => array('container'), '#field_name' => $field_name, '#cardinality' => $cardinality, '#cardinality_multiple' => TRUE, '#max_delta' => $max - 1, 'text' => array('#markup' => $element_text));
     }
     // Add 'add more' button, if not working with a programmed form.
     if (($real_item_count < $cardinality || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) && !$form_state->isProgrammed()) {
         // Hide the button when translating.
         $add_more_access = $this->getCurrentLangcode($form_state, $items) == $items->getEntity()->getUntranslated()->language()->getId();
         $elements['add_more'] = array('#type' => 'container', '#theme_wrappers' => array('paragraphs_dropbutton_wrapper'), '#access' => $add_more_access);
         if (count($access_options)) {
             if ($this->getSetting('add_mode') == 'button' || $this->getSetting('add_mode') == 'dropdown' && count($access_options) === 1) {
                 foreach ($access_options as $machine_name => $label) {
                     $elements['add_more']['add_more_button_' . $machine_name] = array('#type' => 'submit', '#name' => strtr($id_prefix, '-', '_') . '_' . $machine_name . '_add_more', '#value' => t('Add @type', array('@type' => $label)), '#attributes' => array('class' => array('field-add-more-submit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#submit' => array(array(get_class($this), 'addMoreSubmit')), '#ajax' => array('callback' => array(get_class($this), 'addMoreAjax'), 'wrapper' => $wrapper_id, 'effect' => 'fade'), '#bundle_machine_name' => $machine_name);
                 }
             } elseif ($this->getSetting('add_mode') == 'dropdown') {
                 foreach ($access_options as $machine_name => $label) {
                     $elements['add_more']['add_more_button_' . $machine_name] = array('#type' => 'submit', '#name' => strtr($id_prefix, '-', '_') . '_' . $machine_name . '_add_more', '#value' => t('Add @type', array('@type' => $label)), '#attributes' => array('class' => array('field-add-more-submit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#submit' => array(array(get_class($this), 'addMoreSubmit')), '#ajax' => array('callback' => array(get_class($this), 'addMoreAjax'), 'wrapper' => $wrapper_id, 'effect' => 'fade'), '#bundle_machine_name' => $machine_name, '#prefix' => '<li>', '#suffix' => '</li>');
                 }
                 $elements['add_more']['#theme_wrappers'] = array('dropbutton_wrapper', 'paragraphs_dropbutton_wrapper');
                 $elements['add_more']['prefix'] = array('#markup' => '<ul class="dropbutton">', '#weight' => -999);
                 $elements['add_more']['suffix'] = array('#markup' => '</ul>', '#weight' => 999);
             } else {
                 $elements['add_more']['add_more_select'] = array('#type' => 'select', '#options' => $options, '#title' => t('@title type', array('@title' => $this->getSetting('title'))), '#label_display' => 'hidden');
                 $text = t('Add @title', array('@title' => $this->getSetting('title')));
                 if ($real_item_count > 0) {
                     $text = t('Add another @title', array('@title' => $this->getSetting('title')));
                 }
                 $elements['add_more']['add_more_button'] = array('#type' => 'submit', '#name' => strtr($id_prefix, '-', '_') . '_add_more', '#value' => $text, '#attributes' => array('class' => array('field-add-more-submit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#submit' => array(array(get_class($this), 'addMoreSubmit')), '#ajax' => array('callback' => array(get_class($this), 'addMoreAjax'), 'wrapper' => $wrapper_id, 'effect' => 'fade'));
             }
         } else {
             if (count($options)) {
                 $elements['add_more']['info'] = array('#type' => 'markup', '#markup' => '<em>' . t('You are not allowed to add any of the @title types.', array('@title' => $this->getSetting('title'))) . '</em>');
             } else {
//.........这里部分代码省略.........
开发者ID:frankcr,项目名称:sftw8,代码行数:101,代码来源:InlineParagraphsWidget.php


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