Loading config/schema/display_fields.schema.yml +0 −1 Original line number Diff line number Diff line Loading @@ -35,7 +35,6 @@ display_fields.display_fields_view.*.*.*: sequence: - type: entity_view_display.third_party.[%key] display_fields.display_fields_config.*.*: type: config_entity label: 'Entity type display fields' Loading display_fields.info.yml +4 −2 Original line number Diff line number Diff line name: Display fields description: Provide pseudo fields (for display) that can be used to sync other fields and display it with another formatter. description: Provides pseudo fields (for display) that can be used to sync other fields and display it with another formatter. core: 8.x core_version_requirement: ^8 || ^9 type: module package: Fields configure: entity.display_fields_config.collection display_fields.module +14 −17 Original line number Diff line number Diff line <?php use Drupal\display_fields\DisplayFields; use Drupal\display_fields\Entity\DisplayFieldsView; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\Display\EntityDisplayInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\display_fields\DisplayFields; /** * Implements hook_entity_view_alter(). */ function display_fields_entity_view_alter(&$build, EntityInterface $entity, EntityDisplayInterface $display) { function display_fields_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { $entity_type = $entity->getEntityTypeId(); $bundle = $entity->bundle(); $view_mode = $display->get('mode'); Loading @@ -28,7 +25,6 @@ function display_fields_entity_view_alter(&$build, EntityInterface $entity, Enti if (empty($display_fields_fields)) { return; } foreach ($display_fields_fields as $key => $display_field) { $display_settings = $display_fields_display->getComponent($key); Loading @@ -40,11 +36,9 @@ function display_fields_entity_view_alter(&$build, EntityInterface $entity, Enti if (!$plugin) { continue; } $build['display_fields_'.$key] = $plugin->getFieldBuild(array($entity), $display_field, $display_settings, $entity, $view_mode, $language); $build['display_fields_'.$key]['#weight'] = $display_settings['weight']; } } /** Loading @@ -64,6 +58,7 @@ function display_fields_entity_view_alter(&$build, EntityInterface $entity, Enti * Example usage: * - Set the 'body' field to be displayed and the 'field_image' field to be * hidden on article nodes in the 'default' display. * * @code * display_fields_get_entity_view_settings('node', 'article', 'default') * ->setComponent('body', array( Loading @@ -83,12 +78,14 @@ function display_fields_entity_view_alter(&$build, EntityInterface $entity, Enti * The view mode, or 'default' to retrieve the 'default' display object for * this bundle. * * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface * @return \Drupal\Core\Entity\Display\EntityDisplayInterface * The entity view display associated to the view mode. */ function display_fields_get_entity_view_settings($entity_type, $bundle, $view_mode) { function display_fields_get_entity_view_settings(string $entity_type, string $bundle, string $view_mode) { // Try loading the display from configuration. $display = entity_load('display_fields_view', $entity_type . '.' . $bundle . '.' . $view_mode); $display = \Drupal::entityTypeManager() ->getStorage('display_fields_view') ->load($entity_type . '.' . $bundle . '.' . $view_mode); // If not found, create a fresh display object. We do not preemptively create // new entity_view_display configuration entries for each existing entity type Loading @@ -96,7 +93,7 @@ function display_fields_get_entity_view_settings($entity_type, $bundle, $view_mo // configuration entries are only created when a display object is explicitly // configured and saved. if (!$display) { $display = entity_create('display_fields_view', array( $display = \Drupal::entityTypeManager()->getStorage('display_fields_view')->create(array( 'targetEntityType' => $entity_type, 'bundle' => $bundle, 'mode' => $view_mode, Loading display_fields.services.yml +4 −1 Original line number Diff line number Diff line Loading @@ -2,3 +2,6 @@ services: plugin.manager.display_fields: class: Drupal\display_fields\Plugin\DisplayFieldsPluginManager parent: default_plugin_manager displayfields.token: class: Drupal\display_fields\AllTokens arguments: ['@module_handler', '@token'] includes/display_fields.field_ui.inc +93 −80 Original line number Diff line number Diff line <?php use Drupal\Core\Url; use Drupal\Core\Link; use Drupal\Core\Form\FormStateInterface; use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\FieldConfigInterface; use Drupal\field_ui\FieldUI; use Drupal\display_fields\DisplayFields; use Drupal\display_fields\Entity\DisplayFieldsConfig; use Drupal\Component\Utility\SafeMarkup; /** * Adds the Display fields to the form field_ui_display. */ function _display_fields_form_field_ui_display(&$form, FormStateInterface $form_state) { global $base_root, $base_path; // Get the entity_type, bundle and view mode. //$entity_type = $form['#entity_type']; //$bundle = $form['#bundle']; //$view_mode = $form_state->getFormObject()->getEntity()->getMode(); _display_fields_form_field_ui_create_vertical_tabs($form); _display_fields_form_field_ui_display_fields_tab($form, $form_state); _display_fields_form_field_ui_display_fields_add_fields_row($form, $form_state); } /** * Add The fields row form, retrieving the $form from the DisplayFields Field plugin. * Add The fields row form, retrieving the $form from the DisplayFields Field * plugin. */ function _display_fields_form_field_ui_display_fields_add_fields_row(&$form, FormStateInterface $form_state) { $entity_type = $form['#entity_type']; Loading @@ -46,19 +38,22 @@ function _display_fields_form_field_ui_display_fields_add_fields_row(&$form, For $display_fields_settings = display_fields_get_entity_view_settings($entity_type, $bundle, $view_mode); $form['#display_fields'] = $display_fields_config->get('display_fields'); foreach ($form['#display_fields'] as $key => $field) { $field_plugin = DisplayFields::getDisplayFieldsField($field['plugin_id'], $entity_type, $bundle); $form['fields']['display_fields_' . $key] = $field_plugin->buildFieldFormRow($key, $field, $display_fields_settings->getComponent($key), $view_mode, $form_state, $form); } $form['actions']['submit']['#submit'][] = '_display_fields_form_field_ui_display_fields_fields_row_submit'; } /** * Submit handler for the DisplayFields rows. * * @param array $form * @param FormStateInterface $form_state * * @throws \Drupal\Core\Entity\EntityStorageException */ function _display_fields_form_field_ui_display_fields_fields_row_submit(array &$form, FormStateInterface $form_state) { $form_values = $form_state->getValues(); Loading @@ -80,7 +75,7 @@ function _display_fields_form_field_ui_display_fields_fields_row_submit(array &$ // were being edited), or have been persisted in $form_state. $plugin_settings = $form_state->get('plugin_settings'); $settings = array(); $settings = []; if (isset($values['settings_edit_form']['settings'])) { $settings = $values['settings_edit_form']['settings']; } Loading @@ -91,13 +86,12 @@ function _display_fields_form_field_ui_display_fields_fields_row_submit(array &$ $settings = $current_options['settings']; } // Default component values. $component_values = array( $component_values = [ 'type' => $values['type'], 'weight' => $values['weight'], 'weight' => $values['weight'] ?? '', 'settings' => $settings, ); ]; // Only formatters have configurable label visibility. if (isset($values['label'])) { Loading @@ -116,18 +110,21 @@ function _display_fields_form_field_ui_display_fields_fields_row_submit(array &$ * Get the vertical tab UI to create DisplayFields field. */ function _display_fields_form_field_ui_display_fields_tab(&$form, FormStateInterface $form_state) { // Get the entity_type, bundle and view mode. $entity_type = $form['#entity_type']; $bundle = $form['#bundle']; $view_mode = $form_state->getFormObject()->getEntity()->getMode(); $form_state->set('entity_type', $entity_type); $form_state->set('bundle', $bundle); $plugin_id = $form_state->getValue(array('display_fields', 'create', 'field_type')); $plugin_id_selected = $form_state->getValue(array('display_fields', 'create', 'plugin_id')); $plugin_id = $form_state->getValue(['display_fields', 'create', 'field_type']); $plugin_id_selected = $form_state->getValue([ 'display_fields', 'create', 'plugin_id', ]); // Add layouts form. $form['display_fields'] = array( $form['display_fields'] = [ '#type' => 'details', '#title' => t('Display fields'), '#collapsible' => TRUE, Loading @@ -135,35 +132,35 @@ function _display_fields_form_field_ui_display_fields_tab(&$form, FormStateInter '#collapsed' => FALSE, '#weight' => 10, '#tree' => TRUE, ); ]; $form['display_fields']['create'] = array( $form['display_fields']['create'] = [ '#type' => 'fieldset', '#prefix' => '<div id="display-fields-create-wrapper">', '#suffix' => '</div>', '#title' => t('Create a new display field'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); ]; $field_type_options = display_fields_get_field_types($entity_type); if (empty($plugin_id) && empty($plugin_id_selected)) { $form['display_fields']['create']['field_type'] = array( $form['display_fields']['create']['field_type'] = [ '#type' => 'select', '#title' => t('Select the type'), '#options' => array(0 => '- ' . t('Select the type') . ' -') + $field_type_options, '#default_value' => isset($display_field_type) ? $display_field_type : null, '#options' => [0 => '- ' . t('Select the type') . ' -'] + $field_type_options, // TODO: $display_field_type n'est jamais initialisé. //'#default_value' => isset($display_field_type) ? $display_field_type : null, '#weight' => -1, '#ajax' => array( '#ajax' => [ 'callback' => '_display_fields_form_field_ui_create_display_fields_callback', 'wrapper' => 'display-fields-create-wrapper', ), ], '#description' => t('Select a field type to see more options.'), '#attributes' => array( '#attributes' => [ 'autocomplete' => 'off', ), ); ], ]; } else { // Get an instance of the field plugin. Loading @@ -174,51 +171,54 @@ function _display_fields_form_field_ui_display_fields_tab(&$form, FormStateInter $plugin = DisplayFields::getDisplayFieldsField($plugin_id, $entity_type, $bundle); $form['display_fields']['create']['#title'] = $field_type_options[$plugin_id]; $form['display_fields']['create']['plugin_form'] = array(); $form['display_fields']['create']['plugin_form'] = $plugin->createForm($form['display_fields']['create']['plugin_form'], $form_state, array('display_fields', 'create', 'plugin_form')); $form['display_fields']['create']['plugin_form'] = []; $form['display_fields']['create']['plugin_form'] = $plugin->createForm($form['display_fields']['create']['plugin_form'], $form_state, [ 'display_fields', 'create', 'plugin_form', ]); $form['display_fields']['create']['plugin_form']['#tree'] = TRUE; $form['display_fields']['create']['plugin_id'] = array( $form['display_fields']['create']['plugin_id'] = [ '#type' => 'value', '#value' => $plugin_id, ); $form['display_fields']['create']['display_field_name'] = array( ]; $form['display_fields']['create']['display_field_name'] = [ '#type' => 'textfield', '#title' => t('Name'), '#maxlength' => 255, '#description' => t('A label to display this on the field overview.'), '#required' => TRUE, '#weight' => -100, ); $form['display_fields']['create']['display_field_machine_name'] = array( ]; $form['display_fields']['create']['display_field_machine_name'] = [ '#type' => 'machine_name', '#title' => t('Display field machine-name'), '#maxlength' => 160, '#description' => t('A unique name to save the display field. It must only contain lowercase letters, numbers and hyphens.'), '#machine_name' => array( '#machine_name' => [ 'exists' => '_display_fields_field_machine_name_exists', 'source' => array('display_fields', 'create', 'display_field_name'), 'source' => ['display_fields', 'create', 'display_field_name'], 'label' => t('Machine name'), ), ], '#required' => TRUE, '#weight' => -99, ); $form['display_fields']['create']['submit'] = array( ]; $form['display_fields']['create']['submit'] = [ '#type' => 'submit', '#value' => t('Save'), '#submit' => array('_display_fields_form_field_ui_create_display_fields_submit'), ); $form['display_fields']['create']['cancel'] = array( '#submit' => ['_display_fields_form_field_ui_create_display_fields_submit'], ]; $form['display_fields']['create']['cancel'] = [ '#type' => 'submit', '#value' => t('Cancel'), '#op' => 'cancel', '#limit_validation_errors' => array(), '#ajax' => array( '#limit_validation_errors' => [], '#ajax' => [ 'callback' => '_display_fields_form_field_ui_create_display_fields_callback', 'wrapper' => 'display-fields-create-wrapper', ), '#submit' => array('_display_fields_form_field_ui_create_display_fields_cancel'), ); ], '#submit' => ['_display_fields_form_field_ui_create_display_fields_cancel'], ]; } } Loading @@ -228,14 +228,21 @@ function _display_fields_form_field_ui_display_fields_tab(&$form, FormStateInter function _display_fields_form_field_ui_create_display_fields_cancel($form, FormStateInterface $form_state) { $triggering_element = $form_state->getTriggeringElement(); if ($triggering_element['#op'] == 'cancel') { $form_state->unsetValue(array('display_fields', 'create', 'field_type')); $form_state->unsetValue(array('display_fields', 'create', 'plugin_id')); $form_state->unsetValue(['display_fields', 'create', 'field_type']); $form_state->unsetValue(['display_fields', 'create', 'plugin_id']); } $form_state->setRebuild(); } /** * Submit handler for the displayFields fields rows plugins settings. * Submit handler for the DisplayFields fields rows plugins settings. * * @param $form * @param \Drupal\Core\Form\FormStateInterface $form_state * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException * @throws \Drupal\Core\Entity\EntityStorageException */ function _display_fields_form_field_ui_create_display_fields_submit($form, FormStateInterface $form_state) { $plugin_id = $form['display_fields']['create']['plugin_id']['#value']; Loading @@ -243,23 +250,29 @@ function _display_fields_form_field_ui_create_display_fields_submit($form, FormS $entity_type = $form['#entity_type']; $bundle = $form['#bundle']; $plugin = DisplayFields::getDisplayFieldsField($plugin_id, $entity_type, $bundle); $plugin->createFormSubmit($form, $form_state, array('display_fields', 'create', 'plugin_form')); $plugin->createFormSubmit($form, $form_state, [ 'display_fields', 'create', 'plugin_form', ]); $values = $form_state->getValue(array('display_fields', 'create')); $values = $form_state->getValue(['display_fields', 'create']); // Try to load the existing config for this entity_type / bundle $display_fields_config = DisplayFields::getDisplayFields($entity_type, $bundle); $display_fields = $display_fields_config->get('display_fields'); $display_fields[$values['display_field_machine_name']] = array( $display_fields[$values['display_field_machine_name']] = [ 'field_name' => $values['display_field_machine_name'], 'label' => $values['display_field_name'], 'plugin_id' => $plugin_id, 'settings' => $values['plugin_form'], ); ]; $display_fields_config->set('display_fields', $display_fields); $display_fields_config->set('id', $entity_type . '.' . $bundle); $display_fields_config->set('label', $entity_type . ' ' . $bundle); $display_fields_config->save(); } Loading @@ -277,7 +290,8 @@ function _display_fields_field_machine_name_exists($value, $element, $form_state } /** * Ajax callback for the display fields vertical tab UI (creation of displayFields). * Ajax callback for the display fields vertical tab UI (creation of * DisplayFields). */ function _display_fields_form_field_ui_create_display_fields_callback($form, FormStateInterface $form_state) { return $form['display_fields']['create']; Loading @@ -287,16 +301,15 @@ function _display_fields_form_field_ui_create_display_fields_callback($form, For * Create or merge vertical tabs. */ function _display_fields_form_field_ui_create_vertical_tabs(&$form) { // Add additional settings vertical tab. if (!isset($form['additional_settings'])) { $form['additional_settings'] = array( $form['additional_settings'] = [ '#type' => 'vertical_tabs', '#theme_wrappers' => array('vertical_tabs'), '#theme_wrappers' => ['vertical_tabs'], '#prefix' => '<div>', '#suffix' => '</div>', '#tree' => TRUE, ); ]; } // Add the view modes statuses settings to the vertical tabs, if exists. Loading @@ -305,8 +318,8 @@ function _display_fields_form_field_ui_create_vertical_tabs(&$form) { $form['modes']['#group'] = 'additional_settings'; $form['modes']['#weight'] = -10; if ($view_mode_admin_access) { $url = \Drupal\Core\Url::fromRoute('field_ui.display_mode'); $form['modes']['view_modes_custom']['#description'] = \Drupal::l(t('Manage display modes'), $url); $url = Url::fromRoute('field_ui.display_mode'); $form['modes']['view_modes_custom']['#description'] = Link::fromTextAndUrl(t('Manage display modes'), $url); } } } Loading
config/schema/display_fields.schema.yml +0 −1 Original line number Diff line number Diff line Loading @@ -35,7 +35,6 @@ display_fields.display_fields_view.*.*.*: sequence: - type: entity_view_display.third_party.[%key] display_fields.display_fields_config.*.*: type: config_entity label: 'Entity type display fields' Loading
display_fields.info.yml +4 −2 Original line number Diff line number Diff line name: Display fields description: Provide pseudo fields (for display) that can be used to sync other fields and display it with another formatter. description: Provides pseudo fields (for display) that can be used to sync other fields and display it with another formatter. core: 8.x core_version_requirement: ^8 || ^9 type: module package: Fields configure: entity.display_fields_config.collection
display_fields.module +14 −17 Original line number Diff line number Diff line <?php use Drupal\display_fields\DisplayFields; use Drupal\display_fields\Entity\DisplayFieldsView; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\Display\EntityDisplayInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\display_fields\DisplayFields; /** * Implements hook_entity_view_alter(). */ function display_fields_entity_view_alter(&$build, EntityInterface $entity, EntityDisplayInterface $display) { function display_fields_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { $entity_type = $entity->getEntityTypeId(); $bundle = $entity->bundle(); $view_mode = $display->get('mode'); Loading @@ -28,7 +25,6 @@ function display_fields_entity_view_alter(&$build, EntityInterface $entity, Enti if (empty($display_fields_fields)) { return; } foreach ($display_fields_fields as $key => $display_field) { $display_settings = $display_fields_display->getComponent($key); Loading @@ -40,11 +36,9 @@ function display_fields_entity_view_alter(&$build, EntityInterface $entity, Enti if (!$plugin) { continue; } $build['display_fields_'.$key] = $plugin->getFieldBuild(array($entity), $display_field, $display_settings, $entity, $view_mode, $language); $build['display_fields_'.$key]['#weight'] = $display_settings['weight']; } } /** Loading @@ -64,6 +58,7 @@ function display_fields_entity_view_alter(&$build, EntityInterface $entity, Enti * Example usage: * - Set the 'body' field to be displayed and the 'field_image' field to be * hidden on article nodes in the 'default' display. * * @code * display_fields_get_entity_view_settings('node', 'article', 'default') * ->setComponent('body', array( Loading @@ -83,12 +78,14 @@ function display_fields_entity_view_alter(&$build, EntityInterface $entity, Enti * The view mode, or 'default' to retrieve the 'default' display object for * this bundle. * * @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface * @return \Drupal\Core\Entity\Display\EntityDisplayInterface * The entity view display associated to the view mode. */ function display_fields_get_entity_view_settings($entity_type, $bundle, $view_mode) { function display_fields_get_entity_view_settings(string $entity_type, string $bundle, string $view_mode) { // Try loading the display from configuration. $display = entity_load('display_fields_view', $entity_type . '.' . $bundle . '.' . $view_mode); $display = \Drupal::entityTypeManager() ->getStorage('display_fields_view') ->load($entity_type . '.' . $bundle . '.' . $view_mode); // If not found, create a fresh display object. We do not preemptively create // new entity_view_display configuration entries for each existing entity type Loading @@ -96,7 +93,7 @@ function display_fields_get_entity_view_settings($entity_type, $bundle, $view_mo // configuration entries are only created when a display object is explicitly // configured and saved. if (!$display) { $display = entity_create('display_fields_view', array( $display = \Drupal::entityTypeManager()->getStorage('display_fields_view')->create(array( 'targetEntityType' => $entity_type, 'bundle' => $bundle, 'mode' => $view_mode, Loading
display_fields.services.yml +4 −1 Original line number Diff line number Diff line Loading @@ -2,3 +2,6 @@ services: plugin.manager.display_fields: class: Drupal\display_fields\Plugin\DisplayFieldsPluginManager parent: default_plugin_manager displayfields.token: class: Drupal\display_fields\AllTokens arguments: ['@module_handler', '@token']
includes/display_fields.field_ui.inc +93 −80 Original line number Diff line number Diff line <?php use Drupal\Core\Url; use Drupal\Core\Link; use Drupal\Core\Form\FormStateInterface; use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\FieldConfigInterface; use Drupal\field_ui\FieldUI; use Drupal\display_fields\DisplayFields; use Drupal\display_fields\Entity\DisplayFieldsConfig; use Drupal\Component\Utility\SafeMarkup; /** * Adds the Display fields to the form field_ui_display. */ function _display_fields_form_field_ui_display(&$form, FormStateInterface $form_state) { global $base_root, $base_path; // Get the entity_type, bundle and view mode. //$entity_type = $form['#entity_type']; //$bundle = $form['#bundle']; //$view_mode = $form_state->getFormObject()->getEntity()->getMode(); _display_fields_form_field_ui_create_vertical_tabs($form); _display_fields_form_field_ui_display_fields_tab($form, $form_state); _display_fields_form_field_ui_display_fields_add_fields_row($form, $form_state); } /** * Add The fields row form, retrieving the $form from the DisplayFields Field plugin. * Add The fields row form, retrieving the $form from the DisplayFields Field * plugin. */ function _display_fields_form_field_ui_display_fields_add_fields_row(&$form, FormStateInterface $form_state) { $entity_type = $form['#entity_type']; Loading @@ -46,19 +38,22 @@ function _display_fields_form_field_ui_display_fields_add_fields_row(&$form, For $display_fields_settings = display_fields_get_entity_view_settings($entity_type, $bundle, $view_mode); $form['#display_fields'] = $display_fields_config->get('display_fields'); foreach ($form['#display_fields'] as $key => $field) { $field_plugin = DisplayFields::getDisplayFieldsField($field['plugin_id'], $entity_type, $bundle); $form['fields']['display_fields_' . $key] = $field_plugin->buildFieldFormRow($key, $field, $display_fields_settings->getComponent($key), $view_mode, $form_state, $form); } $form['actions']['submit']['#submit'][] = '_display_fields_form_field_ui_display_fields_fields_row_submit'; } /** * Submit handler for the DisplayFields rows. * * @param array $form * @param FormStateInterface $form_state * * @throws \Drupal\Core\Entity\EntityStorageException */ function _display_fields_form_field_ui_display_fields_fields_row_submit(array &$form, FormStateInterface $form_state) { $form_values = $form_state->getValues(); Loading @@ -80,7 +75,7 @@ function _display_fields_form_field_ui_display_fields_fields_row_submit(array &$ // were being edited), or have been persisted in $form_state. $plugin_settings = $form_state->get('plugin_settings'); $settings = array(); $settings = []; if (isset($values['settings_edit_form']['settings'])) { $settings = $values['settings_edit_form']['settings']; } Loading @@ -91,13 +86,12 @@ function _display_fields_form_field_ui_display_fields_fields_row_submit(array &$ $settings = $current_options['settings']; } // Default component values. $component_values = array( $component_values = [ 'type' => $values['type'], 'weight' => $values['weight'], 'weight' => $values['weight'] ?? '', 'settings' => $settings, ); ]; // Only formatters have configurable label visibility. if (isset($values['label'])) { Loading @@ -116,18 +110,21 @@ function _display_fields_form_field_ui_display_fields_fields_row_submit(array &$ * Get the vertical tab UI to create DisplayFields field. */ function _display_fields_form_field_ui_display_fields_tab(&$form, FormStateInterface $form_state) { // Get the entity_type, bundle and view mode. $entity_type = $form['#entity_type']; $bundle = $form['#bundle']; $view_mode = $form_state->getFormObject()->getEntity()->getMode(); $form_state->set('entity_type', $entity_type); $form_state->set('bundle', $bundle); $plugin_id = $form_state->getValue(array('display_fields', 'create', 'field_type')); $plugin_id_selected = $form_state->getValue(array('display_fields', 'create', 'plugin_id')); $plugin_id = $form_state->getValue(['display_fields', 'create', 'field_type']); $plugin_id_selected = $form_state->getValue([ 'display_fields', 'create', 'plugin_id', ]); // Add layouts form. $form['display_fields'] = array( $form['display_fields'] = [ '#type' => 'details', '#title' => t('Display fields'), '#collapsible' => TRUE, Loading @@ -135,35 +132,35 @@ function _display_fields_form_field_ui_display_fields_tab(&$form, FormStateInter '#collapsed' => FALSE, '#weight' => 10, '#tree' => TRUE, ); ]; $form['display_fields']['create'] = array( $form['display_fields']['create'] = [ '#type' => 'fieldset', '#prefix' => '<div id="display-fields-create-wrapper">', '#suffix' => '</div>', '#title' => t('Create a new display field'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); ]; $field_type_options = display_fields_get_field_types($entity_type); if (empty($plugin_id) && empty($plugin_id_selected)) { $form['display_fields']['create']['field_type'] = array( $form['display_fields']['create']['field_type'] = [ '#type' => 'select', '#title' => t('Select the type'), '#options' => array(0 => '- ' . t('Select the type') . ' -') + $field_type_options, '#default_value' => isset($display_field_type) ? $display_field_type : null, '#options' => [0 => '- ' . t('Select the type') . ' -'] + $field_type_options, // TODO: $display_field_type n'est jamais initialisé. //'#default_value' => isset($display_field_type) ? $display_field_type : null, '#weight' => -1, '#ajax' => array( '#ajax' => [ 'callback' => '_display_fields_form_field_ui_create_display_fields_callback', 'wrapper' => 'display-fields-create-wrapper', ), ], '#description' => t('Select a field type to see more options.'), '#attributes' => array( '#attributes' => [ 'autocomplete' => 'off', ), ); ], ]; } else { // Get an instance of the field plugin. Loading @@ -174,51 +171,54 @@ function _display_fields_form_field_ui_display_fields_tab(&$form, FormStateInter $plugin = DisplayFields::getDisplayFieldsField($plugin_id, $entity_type, $bundle); $form['display_fields']['create']['#title'] = $field_type_options[$plugin_id]; $form['display_fields']['create']['plugin_form'] = array(); $form['display_fields']['create']['plugin_form'] = $plugin->createForm($form['display_fields']['create']['plugin_form'], $form_state, array('display_fields', 'create', 'plugin_form')); $form['display_fields']['create']['plugin_form'] = []; $form['display_fields']['create']['plugin_form'] = $plugin->createForm($form['display_fields']['create']['plugin_form'], $form_state, [ 'display_fields', 'create', 'plugin_form', ]); $form['display_fields']['create']['plugin_form']['#tree'] = TRUE; $form['display_fields']['create']['plugin_id'] = array( $form['display_fields']['create']['plugin_id'] = [ '#type' => 'value', '#value' => $plugin_id, ); $form['display_fields']['create']['display_field_name'] = array( ]; $form['display_fields']['create']['display_field_name'] = [ '#type' => 'textfield', '#title' => t('Name'), '#maxlength' => 255, '#description' => t('A label to display this on the field overview.'), '#required' => TRUE, '#weight' => -100, ); $form['display_fields']['create']['display_field_machine_name'] = array( ]; $form['display_fields']['create']['display_field_machine_name'] = [ '#type' => 'machine_name', '#title' => t('Display field machine-name'), '#maxlength' => 160, '#description' => t('A unique name to save the display field. It must only contain lowercase letters, numbers and hyphens.'), '#machine_name' => array( '#machine_name' => [ 'exists' => '_display_fields_field_machine_name_exists', 'source' => array('display_fields', 'create', 'display_field_name'), 'source' => ['display_fields', 'create', 'display_field_name'], 'label' => t('Machine name'), ), ], '#required' => TRUE, '#weight' => -99, ); $form['display_fields']['create']['submit'] = array( ]; $form['display_fields']['create']['submit'] = [ '#type' => 'submit', '#value' => t('Save'), '#submit' => array('_display_fields_form_field_ui_create_display_fields_submit'), ); $form['display_fields']['create']['cancel'] = array( '#submit' => ['_display_fields_form_field_ui_create_display_fields_submit'], ]; $form['display_fields']['create']['cancel'] = [ '#type' => 'submit', '#value' => t('Cancel'), '#op' => 'cancel', '#limit_validation_errors' => array(), '#ajax' => array( '#limit_validation_errors' => [], '#ajax' => [ 'callback' => '_display_fields_form_field_ui_create_display_fields_callback', 'wrapper' => 'display-fields-create-wrapper', ), '#submit' => array('_display_fields_form_field_ui_create_display_fields_cancel'), ); ], '#submit' => ['_display_fields_form_field_ui_create_display_fields_cancel'], ]; } } Loading @@ -228,14 +228,21 @@ function _display_fields_form_field_ui_display_fields_tab(&$form, FormStateInter function _display_fields_form_field_ui_create_display_fields_cancel($form, FormStateInterface $form_state) { $triggering_element = $form_state->getTriggeringElement(); if ($triggering_element['#op'] == 'cancel') { $form_state->unsetValue(array('display_fields', 'create', 'field_type')); $form_state->unsetValue(array('display_fields', 'create', 'plugin_id')); $form_state->unsetValue(['display_fields', 'create', 'field_type']); $form_state->unsetValue(['display_fields', 'create', 'plugin_id']); } $form_state->setRebuild(); } /** * Submit handler for the displayFields fields rows plugins settings. * Submit handler for the DisplayFields fields rows plugins settings. * * @param $form * @param \Drupal\Core\Form\FormStateInterface $form_state * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException * @throws \Drupal\Core\Entity\EntityStorageException */ function _display_fields_form_field_ui_create_display_fields_submit($form, FormStateInterface $form_state) { $plugin_id = $form['display_fields']['create']['plugin_id']['#value']; Loading @@ -243,23 +250,29 @@ function _display_fields_form_field_ui_create_display_fields_submit($form, FormS $entity_type = $form['#entity_type']; $bundle = $form['#bundle']; $plugin = DisplayFields::getDisplayFieldsField($plugin_id, $entity_type, $bundle); $plugin->createFormSubmit($form, $form_state, array('display_fields', 'create', 'plugin_form')); $plugin->createFormSubmit($form, $form_state, [ 'display_fields', 'create', 'plugin_form', ]); $values = $form_state->getValue(array('display_fields', 'create')); $values = $form_state->getValue(['display_fields', 'create']); // Try to load the existing config for this entity_type / bundle $display_fields_config = DisplayFields::getDisplayFields($entity_type, $bundle); $display_fields = $display_fields_config->get('display_fields'); $display_fields[$values['display_field_machine_name']] = array( $display_fields[$values['display_field_machine_name']] = [ 'field_name' => $values['display_field_machine_name'], 'label' => $values['display_field_name'], 'plugin_id' => $plugin_id, 'settings' => $values['plugin_form'], ); ]; $display_fields_config->set('display_fields', $display_fields); $display_fields_config->set('id', $entity_type . '.' . $bundle); $display_fields_config->set('label', $entity_type . ' ' . $bundle); $display_fields_config->save(); } Loading @@ -277,7 +290,8 @@ function _display_fields_field_machine_name_exists($value, $element, $form_state } /** * Ajax callback for the display fields vertical tab UI (creation of displayFields). * Ajax callback for the display fields vertical tab UI (creation of * DisplayFields). */ function _display_fields_form_field_ui_create_display_fields_callback($form, FormStateInterface $form_state) { return $form['display_fields']['create']; Loading @@ -287,16 +301,15 @@ function _display_fields_form_field_ui_create_display_fields_callback($form, For * Create or merge vertical tabs. */ function _display_fields_form_field_ui_create_vertical_tabs(&$form) { // Add additional settings vertical tab. if (!isset($form['additional_settings'])) { $form['additional_settings'] = array( $form['additional_settings'] = [ '#type' => 'vertical_tabs', '#theme_wrappers' => array('vertical_tabs'), '#theme_wrappers' => ['vertical_tabs'], '#prefix' => '<div>', '#suffix' => '</div>', '#tree' => TRUE, ); ]; } // Add the view modes statuses settings to the vertical tabs, if exists. Loading @@ -305,8 +318,8 @@ function _display_fields_form_field_ui_create_vertical_tabs(&$form) { $form['modes']['#group'] = 'additional_settings'; $form['modes']['#weight'] = -10; if ($view_mode_admin_access) { $url = \Drupal\Core\Url::fromRoute('field_ui.display_mode'); $form['modes']['view_modes_custom']['#description'] = \Drupal::l(t('Manage display modes'), $url); $url = Url::fromRoute('field_ui.display_mode'); $form['modes']['view_modes_custom']['#description'] = Link::fromTextAndUrl(t('Manage display modes'), $url); } } }