Replicate UI: Let user fill out more fields than just title on Replicate Confirm form

Created on 22 May 2020, about 4 years ago
Updated 19 April 2023, about 1 year ago

Use case:
Site user would like to replicate and seed new entity with as much info as possible in its first revision.

Site builder should be able to pick a subset from among all of a given entity's fields which ones to expose on Replicate > Confirm form so that the user can fill out as much info as is known at the moment of Replicate before saving the new entity (and optionally bailing out w/o new entity).

I am working in a basic form alter to provide this in pretty hard-coded manner, but I think there could be value for others with a flexible solution. My hook consists of:

  • Confirm form alter: Add additional field(s) to $form, and add array_unshift($form['actions']['submit']['#submit'], '_my_module_confirm_replicate_ui_entity_fields_apply'); submit handler.
  • Submit handler writes to the $form_state->getFormBuilder()->getEntity() $entity field(s) (the in-memory, original entity, just like Replicate UI does for title/label to add " (Copy)".
  • Replicate UI takes it from there, populating $replicated_entity on the form and hands off to Replicate to save and redirect.

-Bronius

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States texas-bronius

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡©πŸ‡ͺGermany vistree

    Hi, I need exactly the same. Instead of just changing the label / title, I need the option for the editor to choose the correct bundle from my custom entity. Is this somehow possible?

  • πŸ‡©πŸ‡ͺGermany vistree

    Thanx again @texas-bronius for your initial comment. I think I were able to find a working solution for my "NON translatable" custom entity.
    Within a custom module, I added

    use \Drupal\Core\Form\FormStateInterface;
    
    function mymodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
      if ($form_id == 'MYCUSTOMENTITY_FORM_replicate_form') {
        $form['new_bundle'] = [
          '#type' => 'select',
          '#title' => t('Bundle Type'),
          '#default_value' => 'unuiqe',
          '#options' => ['global' => t('Global'), 'unique' => t('Unique')],
          '#description' => t('Select the target bundle type'),
          '#weight' => -1,
        ];
        array_unshift($form['actions']['submit']['#submit'], '_mymodule_replicate_form_submit');
      }
    }
    
    function _mymodule_replicate_form_submit($form, FormStateInterface $form_state) {
      $entity = $form_state->getFormObject()->getEntity();
      $bundle = $form_state->getValue("new_bundle");
      $entity ->set('bundle', $bundle);
    }
    
    

    Hope this is the right way of implementation ;-)

Production build 0.69.0 2024