Example #1
0
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL, $bundle = NULL)
 {
     parent::buildForm($form, $form_state, $entity_type_id, $bundle);
     // Gather bundle information.
     $instances = array_filter(\Drupal::entityManager()->getFieldDefinitions($this->entity_type, $this->bundle), function ($field_definition) {
         return $field_definition instanceof FieldInstanceConfigInterface;
     });
     $field_types = $this->fieldTypeManager->getDefinitions();
     // Field prefix.
     $field_prefix = \Drupal::config('field_ui.settings')->get('field_prefix');
     $form += array('#entity_type' => $this->entity_type, '#bundle' => $this->bundle, '#fields' => array_keys($instances));
     $table = array('#type' => 'field_ui_table', '#tree' => TRUE, '#header' => array($this->t('Label'), array('data' => $this->t('Machine name'), 'class' => array(RESPONSIVE_PRIORITY_MEDIUM)), $this->t('Field type'), $this->t('Operations')), '#regions' => $this->getRegions(), '#attributes' => array('class' => array('field-ui-overview'), 'id' => 'field-overview'));
     // Fields.
     foreach ($instances as $name => $instance) {
         $field_storage = $instance->getFieldStorageDefinition();
         $route_parameters = array($this->bundleEntityType => $this->bundle, 'field_instance_config' => $instance->id());
         $table[$name] = array('#attributes' => array('id' => drupal_html_class($name)), 'label' => array('#markup' => String::checkPlain($instance->getLabel())), 'field_name' => array('#markup' => $instance->getName()), 'type' => array('#type' => 'link', '#title' => $field_types[$field_storage->getType()]['label'], '#route_name' => 'field_ui.storage_edit_' . $this->entity_type, '#route_parameters' => $route_parameters, '#options' => array('attributes' => array('title' => $this->t('Edit field settings.')))));
         $table[$name]['operations']['data'] = array('#type' => 'operations', '#links' => $this->entityManager->getListBuilder('field_instance_config')->getOperations($instance));
         if (!empty($field_storage->locked)) {
             $table[$name]['operations'] = array('#markup' => $this->t('Locked'));
             $table[$name]['#attributes']['class'][] = 'menu-disabled';
         }
     }
     // Gather valid field types.
     $field_type_options = array();
     foreach ($field_types as $name => $field_type) {
         // Skip field types which should not be added via user interface.
         if (empty($field_type['no_ui'])) {
             $field_type_options[$name] = $field_type['label'];
         }
     }
     asort($field_type_options);
     // Additional row: add new field.
     if ($field_type_options) {
         $name = '_add_new_field';
         $table[$name] = array('#attributes' => array('class' => array('add-new')), 'label' => array('#type' => 'textfield', '#title' => $this->t('New field label'), '#title_display' => 'invisible', '#size' => 15, '#description' => $this->t('Label'), '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . $this->t('Add new field') . '</div>', '#suffix' => '</div>'), 'field_name' => array('#type' => 'machine_name', '#title' => $this->t('New field name'), '#title_display' => 'invisible', '#field_prefix' => '<span dir="ltr">' . $field_prefix, '#field_suffix' => '</span>&lrm;', '#size' => 15, '#description' => $this->t('A unique machine-readable name containing letters, numbers, and underscores.'), '#maxlength' => FieldStorageConfig::NAME_MAX_LENGTH - strlen($field_prefix), '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>', '#machine_name' => array('source' => array('fields', $name, 'label'), 'exists' => array($this, 'fieldNameExists'), 'standalone' => TRUE, 'label' => ''), '#required' => FALSE), 'type' => array('#type' => 'select', '#title' => $this->t('Type of new field'), '#title_display' => 'invisible', '#options' => $field_type_options, '#empty_option' => $this->t('- Select a field type -'), '#description' => $this->t('Type of data to store.'), '#attributes' => array('class' => array('field-type-select')), '#cell_attributes' => array('colspan' => 2), '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>'), 'translatable' => array('#type' => 'value', '#value' => TRUE));
     }
     // Additional row: re-use existing field.
     $existing_fields = $this->getExistingFieldOptions();
     if ($existing_fields) {
         // Build list of options.
         $existing_field_options = array();
         foreach ($existing_fields as $field_name => $info) {
             $text = $this->t('@type: @field (@label)', array('@type' => $info['type_label'], '@label' => $info['label'], '@field' => $info['field']));
             $existing_field_options[$field_name] = truncate_utf8($text, 80, FALSE, TRUE);
         }
         asort($existing_field_options);
         $name = '_add_existing_field';
         $table[$name] = array('#attributes' => array('class' => array('add-new')), '#row_type' => 'add_new_field', '#region_callback' => array($this, 'getRowRegion'), 'label' => array('#type' => 'textfield', '#title' => $this->t('Existing field label'), '#title_display' => 'invisible', '#size' => 15, '#description' => $this->t('Label'), '#attributes' => array('class' => array('label-textfield')), '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . $this->t('Re-use existing field') . '</div>', '#suffix' => '</div>'), 'field_name' => array('#type' => 'select', '#title' => $this->t('Existing field to share'), '#title_display' => 'invisible', '#options' => $existing_field_options, '#empty_option' => $this->t('- Select an existing field -'), '#description' => $this->t('Field to share'), '#attributes' => array('class' => array('field-select')), '#cell_attributes' => array('colspan' => 3), '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>'));
     }
     // We can set the 'rows_order' element, needed by theme_field_ui_table(),
     // here instead of a #pre_render callback because this form doesn't have the
     // tabledrag behavior anymore.
     $table['#regions']['content']['rows_order'] = array();
     foreach (Element::children($table) as $name) {
         $table['#regions']['content']['rows_order'][] = $name;
     }
     $form['fields'] = $table;
     $form['actions'] = array('#type' => 'actions');
     $form['actions']['submit'] = array('#type' => 'submit', '#button_type' => 'primary', '#value' => $this->t('Save'));
     return $form;
 }
 /**
  * {@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;
 }