/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { if (!$this->canBuildForm($form_state)) { return $element; } $element['#type'] = 'fieldset'; $this->setIefId(sha1($items->getName() . '-ief-single-' . $delta)); $entity = NULL; if ($items->get($delta)->target_id) { $entity = $items->get($delta)->entity; if (!$entity) { $element['warning']['#markup'] = t('Unable to load the referenced entity.'); return $element; } } $op = isset($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, $language, $delta, $parents, $bundle, $entity, TRUE); return $element; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $element['input'] = $element + array('#type' => 'textfield', '#placeholder' => $this->getSetting('placeholder_url'), '#default_value' => isset($items[$delta]->input) ? $items[$delta]->input : NULL, '#maxlength' => 255, '#element_validate' => array(array($this, 'validateInput'))); if ($element['input']['#description'] == '') { $element['input']['#description'] = t('Enter the YouTube URL. Valid URL formats include: http://www.youtube.com/watch?v=1SqBdS0XkV4 and http://youtu.be/1SqBdS0XkV4'); } if (isset($items->get($delta)->video_id)) { $element['video_id'] = array('#prefix' => '<div class="youtube-video-id">', '#markup' => t('YouTube video ID: @video_id', array('@video_id' => $items->get($delta)->video_id)), '#suffix' => '</div>', '#weight' => 1); } return $element; }
/** * {@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; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { /** @var \Drupal\plugin\Plugin\Field\FieldType\PluginCollectionItemInterface $item */ $item = $items[$delta]; /** @var \Drupal\plugin\PluginType\PluginTypeInterface $plugin_type */ $plugin_type = $item->getPluginType(); $element = ['#delta' => $delta, '#field_definition' => $this->fieldDefinition, '#element_validate' => [[get_class(), 'validateFormElement']], '#plugin_type_id' => $plugin_type->getId(), '#plugin_selector_id' => $this->pluginDefinition['plugin_selector_id'], '#process' => [[get_class(), 'processFormElement']], '#selected_plugin' => $items->isEmpty() ? NULL : $items->get($delta)->getContainedPluginInstance()]; $element['plugin_selector'] = static::getPluginSelector($form_state, $element)->buildSelectorForm([], $form_state); return $element; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { if (!$this->canBuildForm($form_state)) { return $element; } $this->setIefId(sha1($items->getName() . '-ief-single-' . $delta)); $entity_type = $this->getFieldSettings()['target_type']; $element['#type'] = 'fieldset'; if ($items->get($delta)->target_id) { $entity = $this->entityManager->getStorage($entity_type)->load($items->get($delta)->target_id); if ($entity) { $element['inline_entity_form'] = $this->getInlineEntityForm('edit', $entity_type, $items->getParent()->getValue()->language()->getId(), $delta, array_merge($element['#field_parents'], [$items->getName(), $delta, 'inline_entity_form']), reset($this->getFieldSettings()['handler_settings']['target_bundles']), $entity, TRUE); } else { $element['warning']['#markup'] = t('Unable to load referenced entity.'); } } else { $element['inline_entity_form'] = $this->getInlineEntityForm('add', $entity_type, $items->getParent()->getValue()->language()->getId(), $delta, array_merge($element['#field_parents'], [$items->getName(), $delta, 'inline_entity_form']), reset($this->getFieldSettings()['handler_settings']['target_bundles']), NULL, TRUE); } return $element; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { /** @var ContentEntityInterface $entity */ $entity = $items->getEntity(); /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity */ $bundle_entity = $this->entityTypeManager->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle()); if (!$this->moderationInformation->isModeratableEntity($entity)) { // @todo write a test for this. return $element + ['#access' => FALSE]; } $default = $items->get($delta)->target_id ?: $bundle_entity->getThirdPartySetting('workbench_moderation', 'default_moderation_state', FALSE); /** @var \Drupal\workbench_moderation\ModerationStateInterface $default_state */ $default_state = $this->entityTypeManager->getStorage('moderation_state')->load($default); if (!$default || !$default_state) { throw new \UnexpectedValueException(sprintf('The %s bundle has an invalid moderation state configuration, moderation states are enabled but no default is set.', $bundle_entity->label())); } $transitions = $this->validator->getValidTransitions($entity, $this->currentUser); $target_states = []; /** @var ModerationStateTransition $transition */ foreach ($transitions as $transition) { $target_states[$transition->getToState()] = $transition->label(); } // @todo write a test for this. $element += ['#access' => FALSE, '#type' => 'select', '#options' => $target_states, '#default_value' => $default, '#published' => $default ? $default_state->isPublishedState() : FALSE]; // Use the dropbutton. $element['#process'][] = [get_called_class(), 'processActions']; return $element; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $entity = $items->getEntity(); /* @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle_entity */ $bundle_entity = $this->entityTypeManager->getStorage($entity->getEntityType()->getBundleEntityType())->load($entity->bundle()); if (!$this->moderationInformation->isModeratableEntity($entity)) { // @todo write a test for this. return $element + ['#access' => FALSE]; } $options = $this->fieldDefinition->getFieldStorageDefinition()->getOptionsProvider($this->column, $entity)->getSettableOptions($this->currentUser); $default = $items->get($delta)->target_id ?: $bundle_entity->getThirdPartySetting('workbench_moderation', 'default_moderation_state', FALSE); /** @var \Drupal\workbench_moderation\ModerationStateInterface $default_state */ $default_state = ModerationState::load($default); if (!$default || !$default_state) { throw new \UnexpectedValueException(sprintf('The %s bundle has an invalid moderation state configuration, moderation states are enabled but no default is set.', $bundle_entity->label())); } // @todo write a test for this. $from = $this->moderationStateTransitionEntityQuery->condition('stateFrom', $default)->execute(); // Can always keep this one as is. $to[$default] = $default; // @todo write a test for this. $allowed = $bundle_entity->getThirdPartySetting('workbench_moderation', 'allowed_moderation_states', []); if ($from) { /* @var \Drupal\workbench_moderation\ModerationStateTransitionInterface $transition */ foreach ($this->moderationStateTransitionStorage->loadMultiple($from) as $id => $transition) { $to_state = $transition->getToState(); if ($this->currentUser->hasPermission('use ' . $id . ' transition') && in_array($to_state, $allowed, TRUE)) { $to[$to_state] = $to_state; } } } $options = array_intersect_key($options, $to); // @todo write a test for this. $element += ['#access' => count($options), '#type' => 'select', '#options' => $options, '#default_value' => $default, '#published' => $default ? $default_state->isPublishedState() : FALSE]; if ($this->currentUser->hasPermission($this->getAdminPermission($entity->getEntityType())) && count($options)) { // Use the dropbutton. $element['#process'][] = [get_called_class(), 'processActions']; // Don't show in sidebar/body. $element['#access'] = FALSE; } else { // Place the field as a details element in the advanced tab-set in e.g. // the sidebar. $element = ['#type' => 'details', '#group' => 'advanced', '#open' => TRUE, '#weight' => -10, '#title' => t('Moderation state'), 'target_id' => $element]; } return $element; }