本文整理汇总了PHP中Drupal\Core\Field\FieldItemListInterface::getName方法的典型用法代码示例。如果您正苦于以下问题:PHP FieldItemListInterface::getName方法的具体用法?PHP FieldItemListInterface::getName怎么用?PHP FieldItemListInterface::getName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Field\FieldItemListInterface
的用法示例。
在下文中一共展示了FieldItemListInterface::getName方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
// Trick inline_entity_form_form_alter() into attaching the handlers,
// WidgetSubmit will be needed once extractFormValues fills the $form_state.
$parents = array_merge($element['#field_parents'], [$items->getName()]);
$ief_id = sha1(implode('-', $parents));
$form_state->set(['inline_entity_form', $ief_id], []);
$element['#type'] = 'fieldset';
$item = $items->get($delta);
if ($item->target_id && !$item->entity) {
$element['warning']['#markup'] = $this->t('Unable to load the referenced entity.');
return $element;
}
$entity = $item->entity;
$op = $entity ? 'edit' : 'add';
$language = $items->getParent()->getValue()->language()->getId();
$parents = array_merge($element['#field_parents'], [$items->getName(), $delta, 'inline_entity_form']);
$bundle = reset($this->getFieldSetting('handler_settings')['target_bundles']);
$element['inline_entity_form'] = $this->getInlineEntityForm($op, $bundle, $language, $delta, $parents, $entity);
if ($op == 'edit') {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
if (!$entity->access('update')) {
// The user isn't allowed to edit the entity, but still needs to see
// it, to be able to reorder values.
$element['entity_label'] = ['#type' => 'markup', '#markup' => $entity->label()];
// Hide the inline form. getInlineEntityForm() still needed to be
// called because otherwise the field re-ordering doesn't work.
$element['inline_entity_form']['#access'] = FALSE;
}
}
return $element;
}
示例2: 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);
}
示例3: formMultipleElements
/**
* {@inheritdoc}
*/
protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state)
{
// Adjust wrapper identifiers as they are shared between parents and
// children in nested field collections.
$form['#wrapper_id'] = Html::getUniqueID($items->getName());
$elements = parent::formMultipleElements($items, $form, $form_state);
$elements['#prefix'] = '<div id="' . $form['#wrapper_id'] . '">';
$elements['#suffix'] = '</div>';
$elements['add_more']['#ajax']['wrapper'] = $form['#wrapper_id'];
return $elements;
}
示例4: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
if (!$this->canBuildForm($form_state)) {
return $element;
}
$settings = $this->getSettings();
$target_type = $this->getFieldSetting('target_type');
// Get the entity type labels for the UI strings.
$labels = $this->labels();
// Build a parents array for this element's values in the form.
$parents = array_merge($element['#field_parents'], array($items->getName(), 'form'));
// Assign a unique identifier to each IEF widget.
// Since $parents can get quite long, sha1() ensures that every id has
// a consistent and relatively short length while maintaining uniqueness.
$this->setIefId(sha1(implode('-', $parents)));
// Get the langcode of the parent entity.
$parent_langcode = $items->getParent()->getValue()->language()->getId();
// Determine the wrapper ID for the entire element.
$wrapper = 'inline-entity-form-' . $this->getIefId();
$element = array('#type' => 'fieldset', '#tree' => TRUE, '#description' => NULL, '#prefix' => '<div id="' . $wrapper . '">', '#suffix' => '</div>', '#ief_id' => $this->getIefId(), '#ief_root' => TRUE) + $element;
$element['#attached']['library'][] = 'inline_entity_form/widget';
// Initialize the IEF array in form state.
if (!$form_state->has(['inline_entity_form', $this->getIefId(), 'settings'])) {
$form_state->set(['inline_entity_form', $this->getIefId(), 'settings'], $this->getFieldSettings());
}
if (!$form_state->has(['inline_entity_form', $this->getIefId(), 'instance'])) {
$form_state->set(['inline_entity_form', $this->getIefId(), 'instance'], $this->fieldDefinition);
}
if (!$form_state->has(['inline_entity_form', $this->getIefId(), 'form'])) {
$form_state->set(['inline_entity_form', $this->getIefId(), 'form'], NULL);
}
if (!$form_state->has(['inline_entity_form', $this->getIefId(), 'array_parents'])) {
$form_state->set(['inline_entity_form', $this->getIefId(), 'array_parents'], $parents);
}
$entities = $form_state->get(['inline_entity_form', $this->getIefId(), 'entities']);
if (!isset($entities)) {
// Load the entities from the $items array and store them in the form
// state for further manipulation.
$form_state->set(['inline_entity_form', $this->getIefId(), 'entities'], array());
if (count($items)) {
foreach ($items as $delta => $item) {
if ($item->entity && is_object($item->entity)) {
$form_state->set(['inline_entity_form', $this->getIefId(), 'entities', $delta], array('entity' => $item->entity, '_weight' => $delta, 'form' => NULL, 'needs_save' => FALSE));
}
}
}
$entities = $form_state->get(['inline_entity_form', $this->getIefId(), 'entities']);
}
// Remove any leftover data from removed entity references.
foreach ($entities as $key => $value) {
if (!isset($value) || !isset($value['entity'])) {
unset($entities[$key]);
}
}
// Build the "Multiple value" widget.
// TODO - does this belong in #element_validate?
$element['#element_validate'] = [[get_class($this), 'updateRowWeights']];
// Add the required element marker & validation.
if ($element['#required']) {
$element['#element_validate'][] = [get_class($this), 'requiredField'];
}
$element['entities'] = array('#tree' => TRUE, '#theme' => 'inline_entity_form_entity_table', '#entity_type' => $target_type);
// Get the fields that should be displayed in the table.
$target_bundles = $this->getTargetBundles();
$fields = $this->iefHandler->tableFields($target_bundles);
$context = array('parent_entity_type' => $this->fieldDefinition->getTargetEntityTypeId(), 'parent_bundle' => $this->fieldDefinition->getTargetBundle(), 'field_name' => $this->fieldDefinition->getName(), 'entity_type' => $target_type, 'allowed_bundles' => $target_bundles);
$this->moduleHandler->alter('inline_entity_form_table_fields', $fields, $context);
$element['entities']['#table_fields'] = $fields;
$weight_delta = max(ceil(count($entities) * 1.2), 50);
foreach ($entities as $key => $value) {
// Data used by theme_inline_entity_form_entity_table().
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $value['entity'];
$element['entities'][$key]['#entity'] = $value['entity'];
$element['entities'][$key]['#needs_save'] = $value['needs_save'];
// Handle row weights.
$element['entities'][$key]['#weight'] = $value['_weight'];
// First check to see if this entity should be displayed as a form.
if (!empty($value['form'])) {
$element['entities'][$key]['title'] = array();
$element['entities'][$key]['delta'] = array('#type' => 'value', '#value' => $value['_weight']);
// Add the appropriate form.
if ($value['form'] == 'edit') {
$element['entities'][$key]['form'] = ['#type' => 'container', '#attributes' => ['class' => ['ief-form', 'ief-form-row']], 'inline_entity_form' => $this->getInlineEntityForm($value['form'], $parent_langcode, $key, array_merge($parents, ['inline_entity_form', 'entities', $key, 'form']), $entity->bundle(), $entity)];
$element['entities'][$key]['form']['inline_entity_form']['#process'] = [['\\Drupal\\inline_entity_form\\Element\\InlineEntityForm', 'processEntityForm'], [get_class($this), 'addIefSubmitCallbacks'], [get_class($this), 'buildEntityFormActions']];
} elseif ($value['form'] == 'remove') {
$element['entities'][$key]['form'] = ['#type' => 'container', '#attributes' => ['class' => ['ief-form', 'ief-form-row']], '#parents' => array_merge($parents, ['entities', $key, 'form']), '#entity' => $entity, '#ief_id' => $this->getIefId(), '#ief_row_delta' => $key];
$this->buildRemoveForm($element['entities'][$key]['form']);
}
} else {
$row =& $element['entities'][$key];
$row['title'] = array();
$row['delta'] = array('#type' => 'weight', '#delta' => $weight_delta, '#default_value' => $value['_weight'], '#attributes' => array('class' => array('ief-entity-delta')));
// Add an actions container with edit and delete buttons for the entity.
$row['actions'] = array('#type' => 'container', '#attributes' => array('class' => array('ief-entity-operations')));
// Make sure entity_access is not checked for unsaved entities.
$entity_id = $entity->id();
//.........这里部分代码省略.........
示例5: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
/** @var \Drupal\commerce_product\Entity\ProductInterface $product */
$product = $form_state->get('product');
$variations = $this->variationStorage->loadEnabled($product);
if (count($variations) === 0) {
// Nothing to purchase, tell the parent form to hide itself.
$form_state->set('hide_form', TRUE);
$element['variation'] = ['#type' => 'value', '#value' => 0];
return $element;
} elseif (count($variations) === 1) {
// Preselect the only possible variation.
// @todo Limit this behavior to products with no attributes instead.
$selected_variation = reset($variations);
$element['variation'] = ['#type' => 'value', '#value' => $selected_variation->id()];
return $element;
}
// Build the full attribute form.
$wrapper_id = Html::getUniqueId('commerce-product-add-to-cart-form');
$form += ['#wrapper_id' => $wrapper_id, '#prefix' => '<div id="' . $wrapper_id . '">', '#suffix' => '</div>'];
$parents = array_merge($element['#field_parents'], [$items->getName(), $delta]);
$user_input = (array) NestedArray::getValue($form_state->getUserInput(), $parents);
$selected_variation = $this->selectVariationFromUserInput($variations, $user_input);
$element['variation'] = ['#type' => 'value', '#value' => $selected_variation->id()];
$element['attributes'] = ['#type' => 'container', '#attributes' => ['class' => ['attribute-widgets']]];
foreach ($this->getAttributeInfo($selected_variation, $variations) as $field_name => $attribute) {
$element['attributes'][$field_name] = ['#type' => $attribute['type'], '#title' => $attribute['title'], '#options' => $attribute['values'], '#required' => $attribute['required'], '#default_value' => $selected_variation->getAttributeId($field_name), '#ajax' => ['callback' => [get_class($this), 'ajaxRefresh'], 'wrapper' => $form['#wrapper_id']]];
// Convert the _none option into #empty_value.
if (isset($element['attributes'][$field_name]['options']['_none'])) {
if (!$element['attributes'][$field_name]['#required']) {
$element['attributes'][$field_name]['#empty_value'] = '';
}
unset($element['attributes'][$field_name]['options']['_none']);
}
// 1 required value -> Disable the element to skip unneeded ajax calls.
if ($attribute['required'] && count($attribute['values']) === 1) {
$element['attributes'][$field_name]['#disabled'] = TRUE;
}
}
return $element;
}