/** * Get the field group delete route for a given group. * @param \stdClass $group * * @return \Drupal\Core\Url * A URL object. */ public static function getDeleteRoute($group) { $entity_type_id = $group->entity_type; $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); if ($entity_type->get('field_ui_base_route')) { $mode_route_name = ''; $route_parameters = FieldUI::getRouteBundleParameter($entity_type, $group->bundle); $route_parameters['field_group_name'] = $group->group_name; // Get correct route name based on context and mode. if ($group->context == 'form') { $context_route_name = 'form_display'; if ($group->mode != 'default') { $mode_route_name = '.form_mode'; $route_parameters['form_mode_name'] = $group->mode; } } else { $context_route_name = 'display'; if ($group->mode != 'default') { $mode_route_name = '.view_mode'; $route_parameters['view_mode_name'] = $group->mode; } } return new Url('field_ui.field_group_delete_' . $entity_type_id . '.' . $context_route_name . $mode_route_name, $route_parameters); } throw new \InvalidArgumentException('The given group is not a valid.'); }
/** * @covers ::getNextDestination */ public function testGetNextDestinationRouteName() { $destinations = [['route_name' => 'system.admin'], ['route_name' => 'system.admin_content']]; $expected_route_name = 'system.admin'; $expected_query = ['destinations' => [['route_name' => 'system.admin_content']]]; $actual = FieldUI::getNextDestination($destinations); $this->assertSame($expected_route_name, $actual->getRouteName()); $this->assertSame($expected_query, $actual->getOption('query')); }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { $paragraphs_type = $this->entity; $status = $paragraphs_type->save(); if ($status) { drupal_set_message($this->t('Saved the %label Paragraphs type.', array('%label' => $paragraphs_type->label()))); } else { drupal_set_message($this->t('The %label Paragraphs type was not saved.', array('%label' => $paragraphs_type->label()))); } if ($status == SAVED_NEW && \Drupal::moduleHandler()->moduleExists('field_ui') && ($route_info = FieldUI::getOverviewRouteInfo('paragraph', $paragraphs_type->id()))) { $form_state->setRedirectUrl($route_info); } else { $form_state->setRedirect('entity.paragraphs_type.collection'); } }
/** * Overrides \Drupal\field_ui\OverviewBase::submitForm(). */ public function submitForm(array &$form, FormStateInterface $form_state) { $error = FALSE; $form_values = $form_state['values']['fields']; $destinations = array(); // Create new field. if (!empty($form_values['_add_new_field']['field_name'])) { $values = $form_values['_add_new_field']; $field_storage = array('name' => $values['field_name'], 'entity_type' => $this->entity_type, 'type' => $values['type'], 'translatable' => $values['translatable']); $instance = array('field_name' => $values['field_name'], 'entity_type' => $this->entity_type, 'bundle' => $this->bundle, 'label' => $values['label'], 'translatable' => FALSE); // Create the field and instance. try { $this->entityManager->getStorage('field_storage_config')->create($field_storage)->save(); $new_instance = $this->entityManager->getStorage('field_instance_config')->create($instance); $new_instance->save(); // Make sure the field is displayed in the 'default' form mode (using // default widget and settings). It stays hidden for other form modes // until it is explicitly configured. entity_get_form_display($this->entity_type, $this->bundle, 'default')->setComponent($values['field_name'])->save(); // Make sure the field is displayed in the 'default' view mode (using // default formatter and settings). It stays hidden for other view // modes until it is explicitly configured. entity_get_display($this->entity_type, $this->bundle, 'default')->setComponent($values['field_name'])->save(); // Always show the field settings step, as the cardinality needs to be // configured for new fields. $route_parameters = array($this->bundleEntityType => $this->bundle, 'field_instance_config' => $new_instance->id()); $destinations[] = array('route_name' => 'field_ui.storage_edit_' . $this->entity_type, 'route_parameters' => $route_parameters); $destinations[] = array('route_name' => 'field_ui.instance_edit_' . $this->entity_type, 'route_parameters' => $route_parameters); // Store new field information for any additional submit handlers. $form_state['fields_added']['_add_new_field'] = $values['field_name']; } catch (\Exception $e) { $error = TRUE; drupal_set_message($this->t('There was a problem creating field %label: !message', array('%label' => $instance['label'], '!message' => $e->getMessage())), 'error'); } } // Re-use existing field. if (!empty($form_values['_add_existing_field']['field_name'])) { $values = $form_values['_add_existing_field']; $field_name = $values['field_name']; $field_storage = FieldStorageConfig::loadByName($this->entity_type, $field_name); if (!empty($field_storage->locked)) { drupal_set_message($this->t('The field %label cannot be added because it is locked.', array('%label' => $values['label'])), 'error'); } else { $instance = array('field_name' => $field_name, 'entity_type' => $this->entity_type, 'bundle' => $this->bundle, 'label' => $values['label']); try { $new_instance = $this->entityManager->getStorage('field_instance_config')->create($instance); $new_instance->save(); // Make sure the field is displayed in the 'default' form mode (using // default widget and settings). It stays hidden for other form modes // until it is explicitly configured. entity_get_form_display($this->entity_type, $this->bundle, 'default')->setComponent($field_name)->save(); // Make sure the field is displayed in the 'default' view mode (using // default formatter and settings). It stays hidden for other view // modes until it is explicitly configured. entity_get_display($this->entity_type, $this->bundle, 'default')->setComponent($field_name)->save(); $destinations[] = array('route_name' => 'field_ui.instance_edit_' . $this->entity_type, 'route_parameters' => array($this->bundleEntityType => $this->bundle, 'field_instance_config' => $new_instance->id())); // Store new field information for any additional submit handlers. $form_state['fields_added']['_add_existing_field'] = $instance['field_name']; } catch (\Exception $e) { $error = TRUE; drupal_set_message($this->t('There was a problem creating field instance %label: @message.', array('%label' => $instance['label'], '@message' => $e->getMessage())), 'error'); } } } if ($destinations) { $destination = drupal_get_destination(); $destinations[] = $destination['destination']; $next_destination = FieldUI::getNextDestination($destinations, $form_state); if (isset($next_destination['route_name'])) { $form_state->setRedirect($next_destination['route_name'], $next_destination['route_parameters'], $next_destination['options']); } else { $form_state['redirect'] = $next_destination; } } elseif (!$error) { drupal_set_message($this->t('Your settings have been saved.')); } }
/** * Adds a contextual tab to entities. */ public function contextualTab($entity_type, $entity_id) { /** @var $entity EntityInterface */ $entity = entity_load($entity_type, $entity_id); $destination = $entity->urlInfo(); if (!empty($entity->ds_switch->value)) { $view_mode = $entity->ds_switch->value; } else { $view_mode = 'full'; } // Get the manage display URI. $route = FieldUI::getOverviewRouteInfo($entity_type, $entity->bundle()); /** @var $entity_display EntityDisplayBase */ $entity_display = entity_get_display($entity_type, $entity->bundle(), $view_mode); $route_parameters = $route->getRouteParameters(); if ($entity_display->getThirdPartySetting('ds', 'layout')) { $route_parameters['view_mode_name'] = $view_mode; $admin_route_name = "entity.entity_view_display.$entity_type.view_mode"; } else { $admin_route_name = "entity.entity_view_display.$entity_type.default"; } $route->setOption('query', array('destination' => $destination->toString())); $url = new Url($admin_route_name, $route_parameters, $route->getOptions()); return new RedirectResponse($url->toString()); }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { $this->entity->save(); drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->entity->getLabel()))); $request = $this->getRequest(); if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) { $request->query->remove('destinations'); $form_state->setRedirectUrl($next_destination); } else { $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->bundle)); } }
/** * Adds a contextual tab to entities. * * @param RouteMatchInterface $route_match * * @return RedirectResponse */ public function contextualTab(RouteMatchInterface $route_match) { $parameter_name = $route_match->getRouteObject()->getOption('_ds_entity_type_id'); $entity = $route_match->getParameter($parameter_name); $entity_type_id = $entity->getEntityTypeId(); $destination = $entity->toUrl(); if (!empty($entity->ds_switch->value)) { $view_mode = $entity->ds_switch->value; } else { $view_mode = 'full'; } // Get the manage display URI. $route = FieldUI::getOverviewRouteInfo($entity_type_id, $entity->bundle()); /** @var $entity_display EntityDisplayBase */ $entity_display = EntityViewDisplay::load($entity_type_id . '.' . $entity->bundle() . '.' . $view_mode); $route_parameters = $route->getRouteParameters(); if ($entity_display && $entity_display->getThirdPartySetting('ds', 'layout')) { $route_parameters['view_mode_name'] = $view_mode; $admin_route_name = "entity.entity_view_display.{$entity_type_id}.view_mode"; } else { $admin_route_name = "entity.entity_view_display.{$entity_type_id}.default"; } $route->setOption('query', array('destination' => $destination->toString())); $url = new Url($admin_route_name, $route_parameters, $route->getOptions()); return new RedirectResponse($url->toString()); }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $field_storage) { if ($field_storage->isLocked()) { $row['class'] = array('menu-disabled'); $row['data']['id'] = $this->t('@field_name (Locked)', array('@field_name' => $field_storage->getName())); } else { $row['data']['id'] = $field_storage->getName(); } $field_type = $this->fieldTypes[$field_storage->getType()]; $row['data']['type'] = $this->t('@type (module: @module)', array('@type' => $field_type['label'], '@module' => $field_type['provider'])); $usage = array(); foreach ($field_storage->getBundles() as $bundle) { $entity_type_id = $field_storage->getTargetEntityTypeId(); if ($route_info = FieldUI::getOverviewRouteInfo($entity_type_id, $bundle)) { $usage[] = \Drupal::l($this->bundles[$entity_type_id][$bundle]['label'], $route_info); } else { $usage[] = $this->bundles[$entity_type_id][$bundle]['label']; } } $row['data']['usage']['data'] = ['#theme' => 'item_list', '#items' => $usage, '#context' => ['list_style' => 'comma-list']]; return $row; }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $field_definitions = $this->getFieldDefinitions(); $extra_fields = $this->getExtraFields(); $form += array('#entity_type' => $this->entity->getTargetEntityTypeId(), '#bundle' => $this->entity->getTargetBundle(), '#fields' => array_keys($field_definitions), '#extra' => array_keys($extra_fields)); if (empty($field_definitions) && empty($extra_fields) && ($route_info = FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle()))) { drupal_set_message($this->t('There are no fields yet added. You can add new fields on the <a href=":link">Manage fields</a> page.', array(':link' => $route_info->toString())), 'warning'); return $form; } $table = array('#type' => 'field_ui_table', '#header' => $this->getTableHeader(), '#regions' => $this->getRegions(), '#attributes' => array('class' => array('field-ui-overview'), 'id' => 'field-display-overview'), '#tabledrag' => array(array('action' => 'order', 'relationship' => 'sibling', 'group' => 'field-weight'), array('action' => 'match', 'relationship' => 'parent', 'group' => 'field-parent', 'subgroup' => 'field-parent', 'source' => 'field-name'))); // Field rows. foreach ($field_definitions as $field_name => $field_definition) { $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); } $form['fields'] = $table; // Custom display settings. if ($this->entity->getMode() == 'default') { // Only show the settings if there is at least one custom display mode. $display_mode_options = $this->getDisplayModeOptions(); // Unset default option. unset($display_mode_options['default']); if ($display_mode_options) { $form['modes'] = array('#type' => 'details', '#title' => $this->t('Custom display settings')); // Prepare default values for the 'Custom display settings' checkboxes. $default = array(); if ($enabled_displays = array_filter($this->getDisplayStatuses())) { $default = array_keys(array_intersect_key($display_mode_options, $enabled_displays)); } $form['modes']['display_modes_custom'] = array('#type' => 'checkboxes', '#title' => $this->t('Use custom display settings for the following @display_context modes', ['@display_context' => $this->displayContext]), '#options' => $display_mode_options, '#default_value' => $default); // Provide link to manage display modes. $form['modes']['display_modes_link'] = $this->getDisplayModesLink(); } } // In overviews involving nested rows from contributed modules (i.e // field_group), the 'plugin type' selects can trigger a series of changes // in child rows. The #ajax behavior is therefore not attached directly to // the selects, but triggered by the client-side script through a hidden // #ajax 'Refresh' button. A hidden 'refresh_rows' input tracks the name of // affected rows. $form['refresh_rows'] = array('#type' => 'hidden'); $form['refresh'] = array('#type' => 'submit', '#value' => $this->t('Refresh'), '#op' => 'refresh_table', '#submit' => array('::multistepSubmit'), '#ajax' => array('callback' => '::multistepAjax', 'wrapper' => 'field-display-overview-wrapper', 'effect' => 'fade', 'progress' => 'none'), '#attributes' => array('class' => array('visually-hidden'))); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#button_type' => 'primary', '#value' => $this->t('Save')); $form['#attached']['library'][] = 'field_ui/drupal.field_ui'; return $form; }
/** * Get the correct url to redirect to. */ private function getRedirectUrl() { $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId); if ($entity_type->get('field_ui_base_route')) { $context_route_name = ""; $mode_route_name = "default"; $route_parameters = FieldUI::getRouteBundleParameter($entity_type, $this->bundle); // Get correct route name based on context and mode. if ($this->context == 'form') { $context_route_name = 'entity_form_display'; if ($this->mode != 'default') { $mode_route_name = 'form_mode'; $route_parameters['form_mode_name'] = $this->mode; } } else { $context_route_name = 'entity_view_display'; if ($this->mode != 'default') { $mode_route_name = 'view_mode'; $route_parameters['view_mode_name'] = $this->mode; } } return new Url("entity.{$context_route_name}.{$this->entityTypeId}.{$mode_route_name}", $route_parameters); } }
/** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { $field_label = $form_state->get('field_config')->label(); try { $this->entity->save(); drupal_set_message($this->t('Updated field %label field settings.', array('%label' => $field_label))); $request = $this->getRequest(); if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) { $request->query->remove('destinations'); $form_state->setRedirectUrl($next_destination); } else { $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($form_state->get('entity_type_id'), $form_state->get('bundle'))); } } catch (\Exception $e) { drupal_set_message($this->t('Attempt to update field %label failed: %message.', array('%label' => $field_label, '%message' => $e->getMessage())), 'error'); } }
/** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { // Handle the default value. $default_value = array(); if (isset($form['instance']['default_value'])) { $items = $form['#entity']->get($this->instance->getName()); $default_value = $items->defaultValuesFormSubmit($form['instance']['default_value'], $form, $form_state); } $this->instance->default_value = $default_value; // Merge incoming values into the instance. foreach ($form_state['values']['instance'] as $key => $value) { $this->instance->{$key} = $value; } $this->instance->save(); drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->instance->getLabel()))); $request = $this->getRequest(); if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) { $request->query->remove('destinations'); if (isset($next_destination['route_name'])) { $form_state['redirect_route'] = $next_destination; } else { $form_state['redirect'] = $next_destination; } } else { $form_state['redirect_route'] = FieldUI::getOverviewRouteInfo($this->instance->entity_type, $this->instance->bundle); } }
/** * {@inheritdoc} */ public function getCancelUrl() { return FieldUI::getOverviewRouteInfo($this->entity->getTargetEntityTypeId(), $this->entity->getTargetBundle()); }
/** * {@inheritdoc} */ public function getCancelUrl() { return FieldUI::getOverviewRouteInfo($this->entity->entity_type, $this->entity->bundle); }
/** * Form submission handler to redirect to Manage fields page of Field UI. */ public function redirectToFieldUI(array $form, FormStateInterface $form_state) { if ($form_state->getTriggeringElement()['#parents'][0] === 'save_continue' && ($route_info = FieldUI::getOverviewRouteInfo('profile', $this->entity->id()))) { $form_state->setRedirectUrl($route_info); } }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $field_config) { /** @var \Drupal\field\FieldConfigInterface $field_config */ $field_storage = $field_config->getFieldStorageDefinition(); $route_parameters = array('field_config' => $field_config->id()) + FieldUI::getRouteBundleParameter($this->entityManager->getDefinition($this->targetEntityTypeId), $this->targetBundle); $row = array('id' => Html::getClass($field_config->getName()), 'data' => array('label' => $field_config->getLabel(), 'field_name' => $field_config->getName(), 'field_type' => array('data' => array('#type' => 'link', '#title' => $this->fieldTypeManager->getDefinitions()[$field_storage->getType()]['label'], '#url' => Url::fromRoute("entity.field_config.{$this->targetEntityTypeId}_storage_edit_form", $route_parameters), '#options' => array('attributes' => array('title' => $this->t('Edit field settings.'))))))); // Add the operations. $row['data'] = $row['data'] + parent::buildRow($field_config); if ($field_storage->isLocked()) { $row['data']['operations'] = array('data' => array('#markup' => $this->t('Locked'))); $row['class'][] = 'menu-disabled'; } return $row; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL, $mode = 'default') { parent::buildForm($form, $form_state, $entity_type_id, $bundle); $this->mode = $mode; $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, '#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))) { drupal_set_message($this->t('There are no fields yet added. You can add new fields on the <a href="@link">Manage fields</a> page.', array('@link' => $route_info->toString())), 'warning'); return $form; } $table = array('#type' => 'field_ui_table', '#pre_render' => array(array($this, 'tablePreRender')), '#tree' => TRUE, '#header' => $this->getTableHeader(), '#regions' => $this->getRegions(), '#attributes' => array('class' => array('field-ui-overview'), 'id' => 'field-display-overview'), '#prefix' => '<div id="field-display-overview-wrapper">', '#suffix' => '</div>', '#tabledrag' => array(array('action' => 'order', 'relationship' => 'sibling', 'group' => 'field-weight'), array('action' => 'match', 'relationship' => 'parent', 'group' => 'field-parent', 'subgroup' => 'field-parent', 'source' => 'field-name'))); // Field rows. foreach ($field_definitions as $field_name => $field_definition) { $table[$field_name] = $this->buildFieldRow($field_definition, $entity_display, $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); } $form['fields'] = $table; // Custom display settings. if ($this->mode == 'default') { // Only show the settings if there is at least one custom display mode. if ($display_modes = $this->getDisplayModes()) { $form['modes'] = array('#type' => 'details', '#title' => $this->t('Custom display settings')); // Collect options and default values for the 'Custom display settings' // checkboxes. $options = array(); $default = array(); $display_statuses = $this->getDisplayStatuses(); foreach ($display_modes as $mode_name => $mode_info) { $options[$mode_name] = $mode_info['label']; if (!empty($display_statuses[$mode_name])) { $default[] = $mode_name; } } $form['modes']['display_modes_custom'] = array('#type' => 'checkboxes', '#title' => $this->t('Use custom display settings for the following modes'), '#options' => $options, '#default_value' => $default); } } // In overviews involving nested rows from contributed modules (i.e // field_group), the 'plugin type' selects can trigger a series of changes // in child rows. The #ajax behavior is therefore not attached directly to // the selects, but triggered by the client-side script through a hidden // #ajax 'Refresh' button. A hidden 'refresh_rows' input tracks the name of // affected rows. $form['refresh_rows'] = array('#type' => 'hidden'); $form['refresh'] = array('#type' => 'submit', '#value' => $this->t('Refresh'), '#op' => 'refresh_table', '#submit' => array(array($this, 'multistepSubmit')), '#ajax' => array('callback' => array($this, 'multistepAjax'), 'wrapper' => 'field-display-overview-wrapper', 'effect' => 'fade', 'progress' => 'none'), '#attributes' => array('class' => array('visually-hidden'))); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#button_type' => 'primary', '#value' => $this->t('Save')); $form['#attached']['library'][] = 'field_ui/drupal.field_ui'; return $form; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $form_values = $form_state->getValues(); $field_values = $form_values['field_storage']; // Save field cardinality. $cardinality = $field_values['cardinality']; $cardinality_number = $field_values['cardinality_number']; if ($cardinality === 'number') { $cardinality = $cardinality_number; } $field_values['cardinality'] = $cardinality; unset($field_values['container']); // Merge incoming form values into the existing field. $field_storage = $this->field->getFieldStorageDefinition(); foreach ($field_values as $key => $value) { $field_storage->set($key, $value); } // Update the field. try { $field_storage->save(); drupal_set_message($this->t('Updated field %label field settings.', array('%label' => $this->field->label()))); $request = $this->getRequest(); if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) { $request->query->remove('destinations'); $form_state->setRedirectUrl($next_destination); } else { $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->field->entity_type, $this->field->bundle)); } } catch (\Exception $e) { drupal_set_message($this->t('Attempt to update field %label failed: %message.', array('%label' => $this->field->label(), '%message' => $e->getMessage())), 'error'); } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Handle the default value. $default_value = array(); if (isset($form['field']['default_value'])) { $items = $form['#entity']->get($this->field->getName()); $default_value = $items->defaultValuesFormSubmit($form['field']['default_value'], $form, $form_state); } $this->field->default_value = $default_value; // Merge incoming values into the field. foreach ($form_state->getValue('field') as $key => $value) { $this->field->set($key, $value); } $this->field->save(); drupal_set_message($this->t('Saved %label configuration.', array('%label' => $this->field->getLabel()))); $request = $this->getRequest(); if (($destinations = $request->query->get('destinations')) && ($next_destination = FieldUI::getNextDestination($destinations))) { $request->query->remove('destinations'); $form_state->setRedirectUrl($next_destination); } else { $form_state->setRedirectUrl(FieldUI::getOverviewRouteInfo($this->field->entity_type, $this->field->bundle)); } }
/** * {@inheritdoc} */ public function buildRow(EntityInterface $field) { if ($field->locked) { $row['class'] = array('menu-disabled'); $row['data']['id'] = t('@field_name (Locked)', array('@field_name' => $field->name)); } else { $row['data']['id'] = $field->name; } $field_type = $this->fieldTypes[$field->type]; $row['data']['type'] = t('@type (module: @module)', array('@type' => $field_type['label'], '@module' => $field_type['provider'])); $usage = array(); foreach ($field->getBundles() as $bundle) { if ($route_info = FieldUI::getOverviewRouteInfo($field->entity_type, $bundle)) { $usage[] = \Drupal::linkGenerator()->generateFromUrl($this->bundles[$field->entity_type][$bundle]['label'], $route_info); } else { $usage[] = $this->bundles[$field->entity_type][$bundle]['label']; } } $usage_escaped = ''; $separator = ''; foreach ($usage as $usage_item) { $usage_escaped .= $separator . SafeMarkup::escape($usage_item); $separator = ', '; } $row['data']['usage'] = SafeMarkup::set($usage_escaped); return $row; }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $error = FALSE; $values = $form_state->getValues(); $destinations = array(); $entity_type = $this->entityManager->getDefinition($this->entityTypeId); // Create new field. if ($values['new_storage_type']) { $field_storage_values = ['field_name' => $values['field_name'], 'entity_type' => $this->entityTypeId, 'type' => $values['new_storage_type'], 'translatable' => $values['translatable']]; $field_values = ['field_name' => $values['field_name'], 'entity_type' => $this->entityTypeId, 'bundle' => $this->bundle, 'label' => $values['label'], 'translatable' => FALSE]; $widget_id = $formatter_id = NULL; // Check if we're dealing with a preconfigured field. if (strpos($field_storage_values['type'], 'field_ui:') !== FALSE) { list(, $field_type, $option_key) = explode(':', $field_storage_values['type'], 3); $field_storage_values['type'] = $field_type; $field_type_class = $this->fieldTypePluginManager->getDefinition($field_type)['class']; $field_options = $field_type_class::getPreconfiguredOptions()[$option_key]; // Merge in preconfigured field storage options. if (isset($field_options['field_storage_config'])) { foreach (array('cardinality', 'settings') as $key) { if (isset($field_options['field_storage_config'][$key])) { $field_storage_values[$key] = $field_options['field_storage_config'][$key]; } } } // Merge in preconfigured field options. if (isset($field_options['field_config'])) { foreach (array('required', 'settings') as $key) { if (isset($field_options['field_config'][$key])) { $field_values[$key] = $field_options['field_config'][$key]; } } } $widget_id = isset($field_options['entity_form_display']['type']) ? $field_options['entity_form_display']['type'] : NULL; $formatter_id = isset($field_options['entity_view_display']['type']) ? $field_options['entity_view_display']['type'] : NULL; } // Create the field storage and field. try { $this->entityManager->getStorage('field_storage_config')->create($field_storage_values)->save(); $field = $this->entityManager->getStorage('field_config')->create($field_values); $field->save(); $this->configureEntityFormDisplay($values['field_name'], $widget_id); $this->configureEntityViewDisplay($values['field_name'], $formatter_id); // Always show the field settings step, as the cardinality needs to be // configured for new fields. $route_parameters = array('field_config' => $field->id()) + FieldUI::getRouteBundleParameter($entity_type, $this->bundle); $destinations[] = array('route_name' => "entity.field_config.{$this->entityTypeId}_storage_edit_form", 'route_parameters' => $route_parameters); $destinations[] = array('route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form", 'route_parameters' => $route_parameters); $destinations[] = array('route_name' => "entity.{$this->entityTypeId}.field_ui_fields", 'route_parameters' => $route_parameters); // Store new field information for any additional submit handlers. $form_state->set(['fields_added', '_add_new_field'], $values['field_name']); } catch (\Exception $e) { $error = TRUE; drupal_set_message($this->t('There was a problem creating field %label: @message', array('%label' => $values['label'], '@message' => $e->getMessage())), 'error'); } } // Re-use existing field. if ($values['existing_storage_name']) { $field_name = $values['existing_storage_name']; try { $field = $this->entityManager->getStorage('field_config')->create(array('field_name' => $field_name, 'entity_type' => $this->entityTypeId, 'bundle' => $this->bundle, 'label' => $values['existing_storage_label'])); $field->save(); $this->configureEntityFormDisplay($field_name); $this->configureEntityViewDisplay($field_name); $route_parameters = array('field_config' => $field->id()) + FieldUI::getRouteBundleParameter($entity_type, $this->bundle); $destinations[] = array('route_name' => "entity.field_config.{$this->entityTypeId}_field_edit_form", 'route_parameters' => $route_parameters); $destinations[] = array('route_name' => "entity.{$this->entityTypeId}.field_ui_fields", 'route_parameters' => $route_parameters); // Store new field information for any additional submit handlers. $form_state->set(['fields_added', '_add_existing_field'], $field_name); } catch (\Exception $e) { $error = TRUE; drupal_set_message($this->t('There was a problem creating field %label: @message', array('%label' => $values['label'], '@message' => $e->getMessage())), 'error'); } } if ($destinations) { $destination = $this->getDestinationArray(); $destinations[] = $destination['destination']; $form_state->setRedirectUrl(FieldUI::getNextDestination($destinations, $form_state)); } elseif (!$error) { drupal_set_message($this->t('Your settings have been saved.')); } }
/** * {@inheritdoc} */ protected function getOverviewUrl($mode) { $entity_type = $this->entityManager->getDefinition($this->entity->getTargetEntityTypeId()); return Url::fromRoute('entity.entity_view_display.' . $this->entity->getTargetEntityTypeId() . '.view_mode', ['view_mode_name' => $mode] + FieldUI::getRouteBundleParameter($entity_type, $this->entity->getTargetBundle())); }
/** * Helper function. * * Display a link to bundle's view mode page if user has permission. * * @param string $entity_type * The type of the entity. * @param string $bundle * The bundle of the entity. * @param string $view_mode * A view mode. * * @return string * A link to the view mode of the bundle if user has access. * The view mode otherwise. */ public function displayViewModeLink($entity_type, $bundle, $view_mode = 'default') { $display = $view_mode; // Get entity type object from entity type name. $entity_type_object = $this->entityTypeManager->getDefinition($entity_type); // Prepare URL parameters. $parameters = array( 'view_mode_name' => $view_mode, ); $parameters += FieldUI::getRouteBundleParameter($entity_type_object, $bundle); // Route. if ($view_mode == 'default') { $route = "entity.entity_view_display.{$entity_type}.default"; } else { $route = "entity.entity_view_display.{$entity_type}.view_mode"; } $url = Url::fromRoute($route, $parameters); if ($url->renderAccess($url->toRenderArray())) { $display = Link::fromTextAndUrl($view_mode, $url); } return $display; }