diff --git a/core/modules/field_ui/field_ui.module b/core/modules/field_ui/field_ui.module index bdd492a..009cb41 100644 --- a/core/modules/field_ui/field_ui.module +++ b/core/modules/field_ui/field_ui.module @@ -88,13 +88,15 @@ function field_ui_entity_type_build(array &$entity_types) { $entity_types['field_config']->setFormClass('delete', 'Drupal\field_ui\Form\FieldConfigDeleteForm'); $entity_types['field_config']->setListBuilderClass('Drupal\field_ui\FieldConfigListBuilder'); $entity_types['field_storage_config']->setListBuilderClass('Drupal\field_ui\FieldStorageConfigListBuilder'); + $entity_types['entity_form_display']->setFormClass('edit', 'Drupal\field_ui\Form\EntityFormDisplayEditForm'); + $entity_types['entity_view_display']->setFormClass('edit', 'Drupal\field_ui\Form\EntityViewDisplayEditForm'); foreach ($entity_types as $entity_type) { if ($bundle = $entity_type->getBundleOf()) { $entity_type ->setLinkTemplate('field_ui-fields', "field_ui.overview_$bundle") - ->setLinkTemplate('field_ui-form-display', "field_ui.form_display_overview_$bundle") - ->setLinkTemplate('field_ui-display', "field_ui.display_overview_$bundle"); + ->setLinkTemplate('field_ui-form-display', "entity.entity_form_display.$bundle.default") + ->setLinkTemplate('field_ui-display', "entity.entity_view_display.$bundle.default"); } } } diff --git a/core/modules/field_ui/src/DisplayOverviewBase.php b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php similarity index 87% rename from core/modules/field_ui/src/DisplayOverviewBase.php rename to core/modules/field_ui/src/Form/EntityDisplayFormBase.php index 09322d3..5a936ab 100644 --- a/core/modules/field_ui/src/DisplayOverviewBase.php +++ b/core/modules/field_ui/src/Form/EntityDisplayFormBase.php @@ -2,44 +2,30 @@ /** * @file - * Contains \Drupal\field_ui\DisplayOverviewBase. + * Contains \Drupal\field_ui\Form\EntityDisplayFormBase. */ -namespace Drupal\field_ui; +namespace Drupal\field_ui\Form; use Drupal\Component\Plugin\Factory\DefaultFactory; use Drupal\Component\Plugin\PluginManagerBase; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\String; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Entity\Display\EntityDisplayInterface; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityForm; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityWithPluginCollectionInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; use Drupal\Core\Field\PluginSettingsInterface; -use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\field_ui\FieldUI; /** - * Field UI display overview base class. + * Base class for EntityDisplay edit forms. */ -abstract class DisplayOverviewBase extends FormBase { - - /** - * The name of the entity type. - * - * @var string - */ - protected $entity_type = ''; - - /** - * The entity bundle. - * - * @var string - */ - protected $bundle = ''; +abstract class EntityDisplayFormBase extends EntityForm { /** * The name of the entity type which provides bundles for the entity type @@ -50,20 +36,6 @@ protected $bundleEntityTypeId; /** - * The entity view or form mode. - * - * @var string - */ - protected $mode = ''; - - /** - * The entity manager. - * - * @var \Drupal\Core\Entity\EntityManagerInterface - */ - protected $entityManager; - - /** * The display context. Either 'view' or 'form'. * * @var string @@ -85,41 +57,41 @@ protected $fieldTypes; /** - * The config factory. + * The entity being used by this form. * - * @var \Drupal\Core\Config\ConfigFactoryInterface + * @var \Drupal\Core\Entity\Display\EntityDisplayInterface */ - protected $configFactory; + protected $entity; /** - * Constructs a new DisplayOverviewBase. + * Constructs a new EntityDisplayFormBase. * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager * The field type manager. * @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager * The widget or formatter plugin manager. - * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory - * The configuration factory. */ - public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager, ConfigFactoryInterface $config_factory) { - $this->entityManager = $entity_manager; + public function __construct(FieldTypePluginManagerInterface $field_type_manager, PluginManagerBase $plugin_manager) { $this->fieldTypes = $field_type_manager->getDefinitions(); $this->pluginManager = $plugin_manager; - $this->configFactory = $config_factory; } /** * {@inheritdoc} */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('entity.manager'), - $container->get('plugin.manager.field.field_type'), - $container->get('plugin.manager.field.widget'), - $container->get('config.factory') - ); + public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) { + $route_parameters = $route_match->getParameters()->all(); + + if (isset($route_parameters['bundle'])) { + $bundle = $route_parameters['bundle']; + } + else { + $target_entity_type = $this->entityManager->getDefinition($route_parameters['entity_type_id']); + $this->bundleEntityTypeId = $target_entity_type->getBundleEntityType(); + $bundle = $route_parameters[$this->bundleEntityTypeId]->id(); + } + + return $this->getEntityDisplay($route_parameters['entity_type_id'], $bundle, $route_parameters[$this->displayContext . '_mode_name']); } /** @@ -177,7 +149,7 @@ public function getRegionOptions() { */ protected function getFieldDefinitions() { $context = $this->displayContext; - return array_filter($this->entityManager->getFieldDefinitions($this->entity_type, $this->bundle), function(FieldDefinitionInterface $field_definition) use ($context) { + return array_filter($this->entityManager->getFieldDefinitions($this->entity->targetEntityType, $this->entity->bundle), function(FieldDefinitionInterface $field_definition) use ($context) { return $field_definition->isDisplayConfigurable($context); }); } @@ -185,32 +157,20 @@ protected function getFieldDefinitions() { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL, $mode = 'default') { - $entity_type = $this->entityManager->getDefinition($entity_type_id); - $this->bundleEntityTypeId = $entity_type->getBundleEntityType(); - - if (!$form_state->get('bundle')) { - $bundle = $bundle ?: $this->getRequest()->attributes->get('_raw_variables')->get($this->bundleEntityTypeId); - $form_state->set('bundle', $bundle); - } - - $this->entity_type = $entity_type_id; - $this->bundle = $form_state->get('bundle'); - $this->mode = $mode; + public function form(array $form, FormStateInterface $form_state) { + $form = parent::form($form, $form_state); $field_definitions = $this->getFieldDefinitions(); $extra_fields = $this->getExtraFields(); - $entity_display = $this->getEntityDisplay($this->mode); $form += array( - '#entity_type' => $this->entity_type, - '#bundle' => $this->bundle, - '#mode' => $this->mode, + '#entity_type' => $this->entity->targetEntityType, + '#bundle' => $this->entity->bundle, '#fields' => array_keys($field_definitions), '#extra' => array_keys($extra_fields), ); - if (empty($field_definitions) && empty($extra_fields) && $route_info = FieldUI::getOverviewRouteInfo($this->entity_type, $this->bundle)) { + if (empty($field_definitions) && empty($extra_fields) && $route_info = FieldUI::getOverviewRouteInfo($this->entity->targetEntityType, $this->entity->bundle)) { drupal_set_message($this->t('There are no fields yet added. You can add new fields on the Manage fields page.', array('@link' => $route_info->toString())), 'warning'); return $form; } @@ -246,18 +206,18 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t // Field rows. foreach ($field_definitions as $field_name => $field_definition) { - $table[$field_name] = $this->buildFieldRow($field_definition, $entity_display, $form, $form_state); + $table[$field_name] = $this->buildFieldRow($field_definition, $form, $form_state); } // Non-field elements. foreach ($extra_fields as $field_id => $extra_field) { - $table[$field_id] = $this->buildExtraFieldRow($field_id, $extra_field, $entity_display); + $table[$field_id] = $this->buildExtraFieldRow($field_id, $extra_field); } $form['fields'] = $table; // Custom display settings. - if ($this->mode == 'default') { + if ($this->entity->mode == 'default') { // Only show the settings if there is at least one custom display mode. if ($display_modes = $this->getDisplayModes()) { $form['modes'] = array( @@ -324,8 +284,6 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t * * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition * The field definition. - * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $entity_display - * The entity display. * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state @@ -334,9 +292,9 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t * @return array * A table row array. */ - protected function buildFieldRow(FieldDefinitionInterface $field_definition, EntityDisplayInterface $entity_display, array $form, FormStateInterface $form_state) { + protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) { $field_name = $field_definition->getName(); - $display_options = $entity_display->getComponent($field_name); + $display_options = $this->entity->getComponent($field_name); $label = $field_definition->getLabel(); $regions = array_keys($this->getRegions()); @@ -514,14 +472,12 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, Ent * The field ID. * @param array $extra_field * The pseudo-field element. - * @param \Drupal\Core\Entity\Display\EntityDisplayInterface $entity_display - * The entity display. * * @return array * A table row array. */ - protected function buildExtraFieldRow($field_id, $extra_field, EntityDisplayInterface $entity_display) { - $display_options = $entity_display->getComponent($field_id); + protected function buildExtraFieldRow($field_id, $extra_field) { + $display_options = $this->entity->getComponent($field_id); $regions = array_keys($this->getRegions()); $extra_field_row = array( @@ -578,8 +534,61 @@ protected function buildExtraFieldRow($field_id, $extra_field, EntityDisplayInte * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + $form_values = $form_state->getValues(); + + // Handle the 'display modes' checkboxes if present. + if ($this->entity->mode == 'default' && !empty($form_values['display_modes_custom'])) { + $display_modes = $this->getDisplayModes(); + $current_statuses = $this->getDisplayStatuses(); + + $statuses = array(); + foreach ($form_values['display_modes_custom'] as $mode => $value) { + if (!empty($value) && empty($current_statuses[$mode])) { + // If no display exists for the newly enabled view mode, initialize + // it with those from the 'default' view mode, which were used so + // far. + if (!$this->entityManager->getStorage($this->entity->getEntityTypeId())->load($this->entity->targetEntityType . '.' . $this->entity->bundle . '.' . $mode)) { + $display = $this->getEntityDisplay($this->entity->targetEntityType, $this->entity->bundle, 'default')->createCopy($mode); + $display->save(); + } + + $display_mode_label = $display_modes[$mode]['label']; + $url = $this->getOverviewUrl($mode); + drupal_set_message($this->t('The %display_mode mode now uses custom display settings. You might want to configure them.', ['%display_mode' => $display_mode_label, '@url' => $url->toString()])); + } + $statuses[$mode] = !empty($value); + } + + $this->saveDisplayStatuses($statuses); + } + + drupal_set_message($this->t('Your settings have been saved.')); + } + + /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + // Work around the fact that entity display are never guaranteed to be + // present, not even the 'default' one. + if (!$this->entityManager->getStorage($this->entity->getEntityTypeId())->load($this->entity->id())) { + $this->entity->enforceIsNew(); + } + + parent::save($form, $form_state); + } + + /** + * {@inheritdoc} + */ + protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { $form_values = $form_state->getValues(); - $display = $this->getEntityDisplay($this->mode); + + if ($this->entity instanceof EntityWithPluginCollectionInterface) { + // Do not manually update values represented by plugin collections. + $form_values = array_diff_key($form_values, $this->entity->getPluginCollections()); + } // Collect data for 'regular' fields. foreach ($form['#fields'] as $field_name) { @@ -588,7 +597,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $values = $form_values['fields'][$field_name]; if ($values['type'] == 'hidden') { - $display->removeComponent($field_name); + $entity->removeComponent($field_name); } else { // Get plugin settings. They lie either directly in submitted form @@ -602,7 +611,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { elseif (isset($plugin_settings[$field_name]['settings'])) { $settings = $plugin_settings[$field_name]['settings']; } - elseif ($current_options = $display->getComponent($field_name)) { + elseif ($current_options = $entity->getComponent($field_name)) { $settings = $current_options['settings']; } $third_party_settings = array(); @@ -612,7 +621,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { elseif (isset($plugin_settings[$field_name]['third_party_settings'])) { $third_party_settings = $plugin_settings[$field_name]['third_party_settings']; } - elseif (($current_options = $display->getComponent($field_name)) && isset($current_options['third_party_settings'])) { + elseif (($current_options = $entity->getComponent($field_name)) && isset($current_options['third_party_settings'])) { $third_party_settings = $current_options['third_party_settings']; } @@ -633,52 +642,21 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $component_values['label'] = $values['label']; } - $display->setComponent($field_name, $component_values); + $entity->setComponent($field_name, $component_values); } } // Collect data for 'extra' fields. foreach ($form['#extra'] as $name) { if ($form_values['fields'][$name]['type'] == 'hidden') { - $display->removeComponent($name); + $entity->removeComponent($name); } else { - $display->setComponent($name, array( + $entity->setComponent($name, array( 'weight' => $form_values['fields'][$name]['weight'], )); } } - - // Save the display. - $display->save(); - - // Handle the 'display modes' checkboxes if present. - if ($this->mode == 'default' && !empty($form_values['display_modes_custom'])) { - $display_modes = $this->getDisplayModes(); - $current_statuses = $this->getDisplayStatuses(); - - $statuses = array(); - foreach ($form_values['display_modes_custom'] as $mode => $value) { - if (!empty($value) && empty($current_statuses[$mode])) { - // If no display exists for the newly enabled view mode, initialize - // it with those from the 'default' view mode, which were used so - // far. - if (!entity_load($this->getEntityDisplay('default')->getEntityTypeId(), $this->entity_type . '.' . $this->bundle . '.' . $mode)) { - $display = $this->getEntityDisplay('default')->createCopy($mode); - $display->save(); - } - - $display_mode_label = $display_modes[$mode]['label']; - $url = $this->getOverviewRoute($mode); - drupal_set_message($this->t('The %display_mode mode now uses custom display settings. You might want to configure them.', ['%display_mode' => $display_mode_label, '@url' => $url->toString()])); - } - $statuses[$mode] = !empty($value); - } - - $this->saveDisplayStatuses($statuses); - } - - drupal_set_message($this->t('Your settings have been saved.')); } /** @@ -865,7 +843,7 @@ public function tablePreRender($elements) { * Determines the rendering order of an array representing a tree. * * Callback for array_reduce() within - * \Drupal\field_ui\DisplayOverviewBase::tablePreRender(). + * \Drupal\field_ui\Form\EntityDisplayFormBase::tablePreRender(). */ public function reduceOrder($array, $a) { $array = !isset($array) ? array() : $array; @@ -880,17 +858,6 @@ public function reduceOrder($array, $a) { } /** - * Returns the entity display object used by this form. - * - * @param string $mode - * A view or form mode. - * - * @return \Drupal\Core\Entity\Display\EntityDisplayInterface - * An entity display. - */ - abstract protected function getEntityDisplay($mode); - - /** * Returns the extra fields of the entity type and bundle used by this form. * * @return array @@ -900,11 +867,26 @@ public function reduceOrder($array, $a) { */ protected function getExtraFields() { $context = $this->displayContext == 'view' ? 'display' : $this->displayContext; - $extra_fields = $this->entityManager->getExtraFields($this->entity_type, $this->bundle); + $extra_fields = $this->entityManager->getExtraFields($this->entity->targetEntityType, $this->entity->bundle); return isset($extra_fields[$context]) ? $extra_fields[$context] : array(); } /** + * Returns an entity display object to be used by this form. + * + * @param string $entity_type_id + * The target entity type ID of the entity display. + * @param string $bundle + * The target bundle of the entity display. + * @param string $mode + * A view or form mode. + * + * @return \Drupal\Core\Entity\Display\EntityDisplayInterface + * An entity display. + */ + abstract protected function getEntityDisplay($entity_type_id, $bundle, $mode); + + /** * Returns the widget or formatter plugin for a field. * * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition @@ -935,7 +917,7 @@ protected function getPluginOptions(FieldDefinitionInterface $field_definition) $applicable_options[$option] = $label; } } - return $applicable_options; + return $applicable_options + array('hidden' => '- ' . $this->t('Hidden') . ' -'); } /** @@ -958,14 +940,6 @@ protected function getPluginOptions(FieldDefinitionInterface $field_definition) abstract protected function getDisplayModes(); /** - * Returns the display entity type. - * - * @return string - * The name of the display entity type. - */ - abstract protected function getDisplayType(); - - /** * Returns the region to which a row in the display overview belongs. * * @param array $row @@ -998,15 +972,15 @@ protected function getExtraFieldVisibilityOptions() { /** * Returns entity (form) displays for the current entity display type. * - * @return array + * @return \Drupal\Core\Entity\Display\EntityDisplayInterface[] * An array holding entity displays or entity form displays. */ protected function getDisplays() { $load_ids = array(); - $display_entity_type = $this->getDisplayType(); + $display_entity_type = $this->entity->getEntityTypeId(); $entity_type = $this->entityManager->getDefinition($display_entity_type); $config_prefix = $entity_type->getConfigPrefix(); - $ids = $this->configFactory->listAll($config_prefix . '.' . $this->entity_type . '.' . $this->bundle . '.'); + $ids = $this->configFactory()->listAll($config_prefix . '.' . $this->entity->targetEntityType . '.' . $this->entity->bundle . '.'); foreach ($ids as $id) { $config_id = str_replace($config_prefix . '.', '', $id); list(,, $display_mode) = explode('.', $config_id); @@ -1014,7 +988,7 @@ protected function getDisplays() { $load_ids[] = $config_id; } } - return entity_load_multiple($display_entity_type, $load_ids); + return $this->entityManager->getStorage($display_entity_type)->loadMultiple($load_ids); } /** @@ -1055,7 +1029,7 @@ protected function saveDisplayStatuses($display_statuses) { abstract protected function getTableHeader(); /** - * Returns the route info of a specific form or view mode form. + * Returns the Url object for a specific entity (form) display edit form. * * @param string $mode * The form or view mode. @@ -1063,7 +1037,7 @@ protected function saveDisplayStatuses($display_statuses) { * @return \Drupal\Core\Url * A Url object for the overview route. */ - abstract protected function getOverviewRoute($mode); + abstract protected function getOverviewUrl($mode); /** * Adds the widget or formatter third party settings forms. diff --git a/core/modules/field_ui/src/FormDisplayOverview.php b/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php similarity index 50% rename from core/modules/field_ui/src/FormDisplayOverview.php rename to core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php index 1728044..5b662fb 100644 --- a/core/modules/field_ui/src/FormDisplayOverview.php +++ b/core/modules/field_ui/src/Form/EntityFormDisplayEditForm.php @@ -2,27 +2,21 @@ /** * @file - * Contains \Drupal\field_ui\FormDisplayOverview. + * Contains \Drupal\field_ui\Form\EntityFormDisplayEditForm. */ -namespace Drupal\field_ui; +namespace Drupal\field_ui\Form; -use Drupal\Component\Plugin\PluginManagerBase; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Entity\Display\EntityDisplayInterface; -use Drupal\Core\Entity\EntityManagerInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\Core\Field\FieldTypePluginManager; use Drupal\Core\Field\PluginSettingsInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Field UI form display overview form. + * Edit form for the EntityFormDisplay entity type. */ -class FormDisplayOverview extends DisplayOverviewBase { +class EntityFormDisplayEditForm extends EntityDisplayFormBase { /** * {@inheritdoc} @@ -30,69 +24,26 @@ class FormDisplayOverview extends DisplayOverviewBase { protected $displayContext = 'form'; /** - * Stores the module manager. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** - * Constructs a new class instance. - * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. - * @param \Drupal\Core\Field\FieldTypePluginManager $field_type_manager - * The field type manager. - * @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager - * The widget or formatter plugin manager. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler to use for invoking hooks. - * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory - * The configuration factory. - */ - public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManager $field_type_manager, PluginManagerBase $plugin_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) { - parent::__construct($entity_manager, $field_type_manager, $plugin_manager, $config_factory); - $this->moduleHandler = $module_handler; - } - - /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager'), $container->get('plugin.manager.field.field_type'), - $container->get('plugin.manager.field.widget'), - $container->get('module_handler'), - $container->get('config.factory') + $container->get('plugin.manager.field.widget') ); } /** * {@inheritdoc} */ - public function getFormId() { - return 'field_ui_form_display_overview_form'; - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL, $form_mode_name = 'default') { - return parent::buildForm($form, $form_state, $entity_type_id, $bundle, $form_mode_name); - } - - /** - * {@inheritdoc} - */ - protected function buildFieldRow(FieldDefinitionInterface $field_definition, EntityDisplayInterface $entity_display, array $form, FormStateInterface $form_state) { - $field_row = parent::buildFieldRow($field_definition, $entity_display, $form, $form_state); + protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) { + $field_row = parent::buildFieldRow($field_definition, $form, $form_state); $field_name = $field_definition->getName(); // Update the (invisible) title of the 'plugin' column. $field_row['plugin']['#title'] = $this->t('Formatter for @title', array('@title' => $field_definition->getLabel())); - if (!empty($field_row['plugin']['settings_edit_form']) && ($plugin = $entity_display->getRenderer($field_name))) { + if (!empty($field_row['plugin']['settings_edit_form']) && ($plugin = $this->entity->getRenderer($field_name))) { $plugin_type_info = $plugin->getPluginDefinition(); $field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Widget settings:') . ' ' . $plugin_type_info['label'] . ''; } @@ -103,8 +54,8 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, Ent /** * {@inheritdoc} */ - protected function getEntityDisplay($mode) { - return entity_get_form_display($this->entity_type, $this->bundle, $mode); + protected function getEntityDisplay($entity_type_id, $bundle, $mode) { + return entity_get_form_display($entity_type_id, $bundle, $mode); } /** @@ -116,7 +67,7 @@ protected function getPlugin(FieldDefinitionInterface $field_definition, $config if ($configuration && $configuration['type'] != 'hidden') { $plugin = $this->pluginManager->getInstance(array( 'field_definition' => $field_definition, - 'form_mode' => $this->mode, + 'form_mode' => $this->entity->mode, 'configuration' => $configuration )); } @@ -127,13 +78,6 @@ protected function getPlugin(FieldDefinitionInterface $field_definition, $config /** * {@inheritdoc} */ - protected function getPluginOptions(FieldDefinitionInterface $field_definition) { - return parent::getPluginOptions($field_definition) + array('hidden' => '- ' . t('Hidden') . ' -'); - } - - /** - * {@inheritdoc} - */ protected function getDefaultPlugin($field_type) { return isset($this->fieldTypes[$field_type]['default_widget']) ? $this->fieldTypes[$field_type]['default_widget'] : NULL; } @@ -142,14 +86,7 @@ protected function getDefaultPlugin($field_type) { * {@inheritdoc} */ protected function getDisplayModes() { - return $this->entityManager->getFormModes($this->entity_type); - } - - /** - * {@inheritdoc} - */ - protected function getDisplayType() { - return 'entity_form_display'; + return $this->entityManager->getFormModes($this->entity->targetEntityType); } /** @@ -167,9 +104,9 @@ protected function getTableHeader() { /** * {@inheritdoc} */ - protected function getOverviewRoute($mode) { - return Url::fromRoute('field_ui.form_display_overview_form_mode_' . $this->entity_type, [ - $this->bundleEntityTypeId => $this->bundle, + protected function getOverviewUrl($mode) { + return Url::fromRoute('entity.entity_form_display.' . $this->entity->targetEntityType . '.form_mode', [ + $this->bundleEntityTypeId => $this->entity->bundle, 'form_mode_name' => $mode, ]); } @@ -185,7 +122,7 @@ protected function thirdPartySettingsForm(PluginSettingsInterface $plugin, Field $settings_form[$module] = $this->moduleHandler->invoke($module, 'field_widget_third_party_settings_form', array( $plugin, $field_definition, - $this->mode, + $this->entity->mode, $form, $form_state, )); @@ -200,7 +137,7 @@ protected function alterSettingsSummary(array &$summary, PluginSettingsInterface $context = array( 'widget' => $plugin, 'field_definition' => $field_definition, - 'form_mode' => $this->mode, + 'form_mode' => $this->entity->mode, ); $this->moduleHandler->alter('field_widget_settings_summary', $summary, $context); } diff --git a/core/modules/field_ui/src/DisplayOverview.php b/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php similarity index 58% rename from core/modules/field_ui/src/DisplayOverview.php rename to core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php index b302529..f358f12 100644 --- a/core/modules/field_ui/src/DisplayOverview.php +++ b/core/modules/field_ui/src/Form/EntityViewDisplayEditForm.php @@ -2,28 +2,21 @@ /** * @file - * Contains \Drupal\field_ui\DisplayOverview. + * Contains \Drupal\field_ui\Form\EntityViewDisplayEditForm. */ -namespace Drupal\field_ui; +namespace Drupal\field_ui\Form; -use Drupal\Component\Plugin\PluginManagerBase; -use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\Core\Entity\Display\EntityDisplayInterface; -use Drupal\Core\Entity\EntityManagerInterface; -use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Field\FieldTypePluginManager; -use Drupal\Core\Field\FormatterInterface; use Drupal\Core\Field\PluginSettingsInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Field UI display overview form. + * Edit form for the EntityViewDisplay entity type. */ -class DisplayOverview extends DisplayOverviewBase { +class EntityViewDisplayEditForm extends EntityDisplayFormBase { /** * {@inheritdoc} @@ -31,66 +24,23 @@ class DisplayOverview extends DisplayOverviewBase { protected $displayContext = 'view'; /** - * Stores the module manager. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** - * Constructs a new class instance. - * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. - * @param \Drupal\Core\Field\FieldTypePluginManager $field_type_manager - * The field type manager. - * @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager - * The widget or formatter plugin manager. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler class to use for invoking hooks. - * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory - * The configuration factory. - */ - public function __construct(EntityManagerInterface $entity_manager, FieldTypePluginManager $field_type_manager, PluginManagerBase $plugin_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) { - parent::__construct($entity_manager, $field_type_manager, $plugin_manager, $config_factory); - $this->moduleHandler = $module_handler; - } - - /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( - $container->get('entity.manager'), $container->get('plugin.manager.field.field_type'), - $container->get('plugin.manager.field.formatter'), - $container->get('module_handler'), - $container->get('config.factory') - ); - } - - /** - * {@inheritdoc} - */ - public function getFormId() { - return 'field_ui_display_overview_form'; - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL, $view_mode_name = 'default') { - return parent::buildForm($form, $form_state, $entity_type_id, $bundle, $view_mode_name); + $container->get('plugin.manager.field.formatter') + ); } /** * {@inheritdoc} */ - protected function buildFieldRow(FieldDefinitionInterface $field_definition, EntityDisplayInterface $entity_display, array $form, FormStateInterface $form_state) { - $field_row = parent::buildFieldRow($field_definition, $entity_display, $form, $form_state); + protected function buildFieldRow(FieldDefinitionInterface $field_definition, array $form, FormStateInterface $form_state) { + $field_row = parent::buildFieldRow($field_definition, $form, $form_state); $field_name = $field_definition->getName(); - $display_options = $entity_display->getComponent($field_name); + $display_options = $this->entity->getComponent($field_name); // Insert the label column. $label = array( @@ -108,7 +58,7 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, Ent // Update the (invisible) title of the 'plugin' column. $field_row['plugin']['#title'] = $this->t('Formatter for @title', array('@title' => $field_definition->getLabel())); - if (!empty($field_row['plugin']['settings_edit_form']) && ($plugin = $entity_display->getRenderer($field_name))) { + if (!empty($field_row['plugin']['settings_edit_form']) && ($plugin = $this->entity->getRenderer($field_name))) { $plugin_type_info = $plugin->getPluginDefinition(); $field_row['plugin']['settings_edit_form']['label']['#markup'] = $this->t('Format settings:') . ' ' . $plugin_type_info['label'] . ''; } @@ -119,8 +69,8 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, Ent /** * {@inheritdoc} */ - protected function buildExtraFieldRow($field_id, $extra_field, EntityDisplayInterface $entity_display) { - $extra_field_row = parent::buildExtraFieldRow($field_id, $extra_field, $entity_display); + protected function buildExtraFieldRow($field_id, $extra_field) { + $extra_field_row = parent::buildExtraFieldRow($field_id, $extra_field); // Insert an empty placeholder for the label column. $label = array( @@ -137,8 +87,8 @@ protected function buildExtraFieldRow($field_id, $extra_field, EntityDisplayInte /** * {@inheritdoc} */ - protected function getEntityDisplay($mode) { - return entity_get_display($this->entity_type, $this->bundle, $mode); + protected function getEntityDisplay($entity_type_id, $bundle, $mode) { + return entity_get_display($entity_type_id, $bundle, $mode); } /** @@ -150,7 +100,7 @@ protected function getPlugin(FieldDefinitionInterface $field_definition, $config if ($configuration && $configuration['type'] != 'hidden') { $plugin = $this->pluginManager->getInstance(array( 'field_definition' => $field_definition, - 'view_mode' => $this->mode, + 'view_mode' => $this->entity->mode, 'configuration' => $configuration )); } @@ -161,13 +111,6 @@ protected function getPlugin(FieldDefinitionInterface $field_definition, $config /** * {@inheritdoc} */ - protected function getPluginOptions(FieldDefinitionInterface $field_definition) { - return parent::getPluginOptions($field_definition) + array('hidden' => '- ' . $this->t('Hidden') . ' -'); - } - - /** - * {@inheritdoc} - */ protected function getDefaultPlugin($field_type) { return isset($this->fieldTypes[$field_type]['default_formatter']) ? $this->fieldTypes[$field_type]['default_formatter'] : NULL; } @@ -176,14 +119,7 @@ protected function getDefaultPlugin($field_type) { * {@inheritdoc} */ protected function getDisplayModes() { - return $this->entityManager->getViewModes($this->entity_type); - } - - /** - * {@inheritdoc} - */ - protected function getDisplayType() { - return 'entity_view_display'; + return $this->entityManager->getViewModes($this->entity->targetEntityType); } /** @@ -202,9 +138,9 @@ protected function getTableHeader() { /** * {@inheritdoc} */ - protected function getOverviewRoute($mode) { - return Url::fromRoute('field_ui.display_overview_view_mode_' . $this->entity_type, [ - $this->bundleEntityTypeId => $this->bundle, + protected function getOverviewUrl($mode) { + return Url::fromRoute('entity.entity_view_display.' . $this->entity->targetEntityType . '.view_mode', [ + $this->bundleEntityTypeId => $this->entity->bundle, 'view_mode_name' => $mode, ]); } @@ -235,7 +171,7 @@ protected function thirdPartySettingsForm(PluginSettingsInterface $plugin, Field $settings_form[$module] = $this->moduleHandler->invoke($module, 'field_formatter_third_party_settings_form', array( $plugin, $field_definition, - $this->mode, + $this->entity->mode, $form, $form_state, )); @@ -250,7 +186,7 @@ protected function alterSettingsSummary(array &$summary, PluginSettingsInterface $context = array( 'formatter' => $plugin, 'field_definition' => $field_definition, - 'view_mode' => $this->mode, + 'view_mode' => $this->entity->mode, ); $this->moduleHandler->alter('field_formatter_settings_summary', $summary, $context); } diff --git a/core/modules/field_ui/src/Plugin/Derivative/FieldUiLocalTask.php b/core/modules/field_ui/src/Plugin/Derivative/FieldUiLocalTask.php index 5d56924..562c2ec 100644 --- a/core/modules/field_ui/src/Plugin/Derivative/FieldUiLocalTask.php +++ b/core/modules/field_ui/src/Plugin/Derivative/FieldUiLocalTask.php @@ -79,7 +79,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { // 'Manage form display' tab. $this->derivatives["form_display_overview_$entity_type_id"] = array( - 'route_name' => "field_ui.form_display_overview_$entity_type_id", + 'route_name' => "entity.entity_form_display.$entity_type_id.default", 'weight' => 2, 'title' => $this->t('Manage form display'), 'base_route' => "field_ui.overview_$entity_type_id", @@ -87,7 +87,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { // 'Manage display' tab. $this->derivatives["display_overview_$entity_type_id"] = array( - 'route_name' => "field_ui.display_overview_$entity_type_id", + 'route_name' => "entity.entity_view_display.$entity_type_id.default", 'weight' => 3, 'title' => $this->t('Manage display'), 'base_route' => "field_ui.overview_$entity_type_id", @@ -116,13 +116,13 @@ public function getDerivativeDefinitions($base_plugin_definition) { // actually visible for a given bundle. $this->derivatives['field_form_display_default_' . $entity_type_id] = array( 'title' => 'Default', - 'route_name' => "field_ui.form_display_overview_$entity_type_id", + 'route_name' => "entity.entity_form_display.$entity_type_id.default", 'parent_id' => "field_ui.fields:form_display_overview_$entity_type_id", 'weight' => -1, ); $this->derivatives['field_display_default_' . $entity_type_id] = array( 'title' => 'Default', - 'route_name' => "field_ui.display_overview_$entity_type_id", + 'route_name' => "entity.entity_view_display.$entity_type_id.default", 'parent_id' => "field_ui.fields:display_overview_$entity_type_id", 'weight' => -1, ); @@ -132,7 +132,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { foreach ($this->entityManager->getFormModes($entity_type_id) as $form_mode => $form_mode_info) { $this->derivatives['field_form_display_' . $form_mode . '_' . $entity_type_id] = array( 'title' => $form_mode_info['label'], - 'route_name' => "field_ui.form_display_overview_form_mode_$entity_type_id", + 'route_name' => "entity.entity_form_display.$entity_type_id.form_mode", 'route_parameters' => array( 'form_mode_name' => $form_mode, ), @@ -146,7 +146,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { foreach ($this->entityManager->getViewModes($entity_type_id) as $view_mode => $form_mode_info) { $this->derivatives['field_display_' . $view_mode . '_' . $entity_type_id] = array( 'title' => $form_mode_info['label'], - 'route_name' => "field_ui.display_overview_view_mode_$entity_type_id", + 'route_name' => "entity.entity_view_display.$entity_type_id.view_mode", 'route_parameters' => array( 'view_mode_name' => $view_mode, ), diff --git a/core/modules/field_ui/src/Routing/RouteSubscriber.php b/core/modules/field_ui/src/Routing/RouteSubscriber.php index 5e9c265..96464c3 100644 --- a/core/modules/field_ui/src/Routing/RouteSubscriber.php +++ b/core/modules/field_ui/src/Routing/RouteSubscriber.php @@ -111,46 +111,48 @@ protected function alterRoutes(RouteCollection $collection) { $route = new Route( "$path/form-display", array( - '_form' => '\Drupal\field_ui\FormDisplayOverview', + '_entity_form' => 'entity_form_display.edit', '_title' => 'Manage form display', + 'form_mode_name' => 'default', ) + $defaults, array('_field_ui_form_mode_access' => 'administer ' . $entity_type_id . ' form display'), $options ); - $collection->add("field_ui.form_display_overview_$entity_type_id", $route); + $collection->add("entity.entity_form_display.$entity_type_id.default", $route); $route = new Route( "$path/form-display/{form_mode_name}", array( - '_form' => '\Drupal\field_ui\FormDisplayOverview', + '_entity_form' => 'entity_form_display.edit', '_title' => 'Manage form display', ) + $defaults, array('_field_ui_form_mode_access' => 'administer ' . $entity_type_id . ' form display'), $options ); - $collection->add("field_ui.form_display_overview_form_mode_$entity_type_id", $route); + $collection->add("entity.entity_form_display.$entity_type_id.form_mode", $route); $route = new Route( "$path/display", array( - '_form' => '\Drupal\field_ui\DisplayOverview', + '_entity_form' => 'entity_view_display.edit', '_title' => 'Manage display', + 'view_mode_name' => 'default', ) + $defaults, array('_field_ui_view_mode_access' => 'administer ' . $entity_type_id . ' display'), $options ); - $collection->add("field_ui.display_overview_$entity_type_id", $route); + $collection->add("entity.entity_view_display.$entity_type_id.default", $route); $route = new Route( "$path/display/{view_mode_name}", array( - '_form' => '\Drupal\field_ui\DisplayOverview', + '_entity_form' => 'entity_view_display.edit', '_title' => 'Manage display', ) + $defaults, array('_field_ui_view_mode_access' => 'administer ' . $entity_type_id . ' display'), $options ); - $collection->add("field_ui.display_overview_view_mode_$entity_type_id", $route); + $collection->add("entity.entity_view_display.$entity_type_id.view_mode", $route); } } } diff --git a/core/modules/field_ui/src/Tests/ManageDisplayTest.php b/core/modules/field_ui/src/Tests/ManageDisplayTest.php index e628757..c09d894 100644 --- a/core/modules/field_ui/src/Tests/ManageDisplayTest.php +++ b/core/modules/field_ui/src/Tests/ManageDisplayTest.php @@ -164,6 +164,7 @@ function testFormatterUI() { // is no longer there. \Drupal::service('module_installer')->uninstall(array('field_third_party_test')); $this->drupalGet($manage_display); + $this->assertResponse(200); $this->assertNoFieldByName('field_test_settings_edit'); } diff --git a/core/modules/node/node.module b/core/modules/node/node.module index ad63d21..5480c39 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -100,13 +100,13 @@ function node_help($route_name, RouteMatchInterface $route_match) { case 'node.type_add': return '

' . t('Individual content types can have different fields, behaviors, and permissions assigned to them.') . '

'; - case 'field_ui.form_display_overview_node': - case 'field_ui.form_display_overview_form_mode_node': + case 'entity.entity_form_display.node.default': + case 'entity.entity_form_display.node.form_mode': $type = $route_match->getParameter('node_type'); return '

' . t('Content items can be edited using different form modes. Here, you can define which fields are shown and hidden when %type content is edited in each form mode, and define how the field form widgets are displayed in each form mode.', array('%type' => $type->label())) . '

' ; - case 'field_ui.display_overview_node': - case 'field_ui.display_overview_view_mode_node': + case 'entity.entity_view_display.node.default': + case 'entity.entity_view_display.node.view_mode': $type = $route_match->getParameter('node_type'); return '

' . t('Content items can be displayed using different view modes: Teaser, Full content, Print, RSS, etc. Teaser is a short format that is typically used in lists of multiple content items. Full content is typically used when the content is displayed on its own page.') . '

' . '

' . t('Here, you can define which fields are shown and hidden when %type content is displayed in each view mode, and define how the fields are displayed in each view mode.', array('%type' => $type->label())) . '

'; diff --git a/core/modules/user/user.module b/core/modules/user/user.module index a251fd7..eb67089 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -84,10 +84,10 @@ function user_help($route_name, RouteMatchInterface $route_match) { case 'field_ui.overview_user': return '

' . t('This form lets administrators add and edit fields for storing user data.') . '

'; - case 'field_ui.form_display_overview_user': + case 'entity.entity_form_display.user.default': return '

' . t('This form lets administrators configure how form fields should be displayed when editing a user profile.') . '

'; - case 'field_ui.display_overview_user': + case 'entity.entity_view_display.user.default': return '

' . t('This form lets administrators configure how fields should be displayed when rendering a user profile page.') . '

'; } }