Is it possible to sort paragraph adding list alphabetically based on Paragraph name (not machine name if thats what is currently used)?

See the attached image, for what is happening currently.
Current list

I expected the following order on the dropdown:

  • 1 Column
  • 2 Column
  • 3 Column
  • Accordion
  • Block
  • Call to action
  • Customer Testimonial
  • Featured Content
  • Header Image
  • Image Left Text Right
  • Image Right Text Left

Issue fork paragraphs-2770243

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

realityloop created an issue. See original summary.

johnchque’s picture

StatusFileSize
new26.86 KB

You can change the weight(order) of the elements of the add button on the field settings form.

miro_dietiker’s picture

Category: Feature request » Support request
Status: Active » Fixed

So yeah, sequence is persisted in paragraphs widget settings.
If you have many paragraph fields, that's a bit of redundant configuration... But i don't see how we could additionally simply maintain a global sequence...

realityloop’s picture

thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

dennis cohn’s picture

Maybe its better to order them alphabetically by default?
It's not logic to show them randomly.. right?

matheuslbarros’s picture

I used something like this (where field_items is the name of the field):

/**
 * Implements hook_form_alter().
 */
function hook_form_alter(&$form, $form_state, $form_id) {
    if (isset($form['field_items'])) {
        ksort($form['field_items']['widget']['add_more']['operations']['#links']);
    }
}
rpsu’s picture

I would indeed be good UX to have the items optionally sorted alphabetically. Especially with long list of paragraphs on multilingual sites it would improve admin UX.

While the hook in #7 solves this, having a checkbox in the field settings would be a good feature to have.

rpsu’s picture

Issue tags: +Usability, +multilingual
ras-ben’s picture

In my case, where I'm using the 'add more' modal, I use this code:

/**
 * Implements hook_form_alter().
 *
 * Sorting the paragraphs in the "add more" display
 * alphabetically.
 */
function EXAMPLE_form_alter(&$form, $form_state, $form_id) {
  $paragraphs_field_name = 'field_paragraphs';

  if (!empty($form[$paragraphs_field_name]['widget']['add_more'])) {
    // Doing an initial sort, as the add_more array has
    // non-keyed/non-field elements. We want those in the
    // edge of the array.
    ksort($form[$paragraphs_field_name]['widget']['add_more']);

    // Doing a sort, based on the label of the paragraph.
    uasort($form[$paragraphs_field_name]['widget']['add_more'], '_EXAMPLE_compare_paragraph_labels');
  }
}

/**
 * Helper function, used to sort paragraph labels.
 *
 * @see EXAMPLE_form_alter()
 */
function _EXAMPLE_compare_paragraph_labels($a, $b) {
  // #value = translated label of paragraph.
  if (empty($a['#value']) || empty($b['#value'])) {
    return 1;
  }

  return strnatcmp($a['#value'], $b['#value']);
}

joachim’s picture

We have a LOT of paragraph types on our project -- one field on a node has 34 types enabled. Using the manual sort order isn't convenient, as every time a new type is added, a developer would have to make sure it's moved into alphabetical order.

I would be inclined to add this as a setting on the form widget, with a note in the description for the setting's form element that it overrides the manual order in the field configuration.

Could this be reopened please?

mhmhartman’s picture

+1 for a form display option to display the selected paragraphs in (alphabetically) ordered fashion. With many paragraphs it's a must have IMO.

Based on ras-ben's answer in comment #10 I could sort my 'add paragraphs' select list, however I needed to make some modifications in order to make it work and prevent errors:

/**
 * Helper function, used to sort paragraph labels.
 *
 * @see EXAMPLE_form_alter()
 */
function _EXAMPLE_compare_paragraph_labels($a, $b) {
  if (is_object($a) || is_object($b)) {
    return 1;
  }
  else if (!isset($a['#value']) || !isset($b['#value'])) {
    return 1;
  }

  return strnatcmp($a['#value']->__tostring(), $b['#value']->__tostring());
}
chrisla’s picture

+1 for this for reasons listed in 11 and 12 above. whenever dealing with lots of paragraphs and a few different paragraphs fields, manually setting the order for them each time is a genuine pain

Above ideas for form_alters do not work for paragraph fields nested within paragraph fields

trishen’s picture

/**
 * Implements hook_form_alter().
 */
function mycustom_form_alter(&$form, FormStateInterface $form_state) {
  $fields = [
    'field_paragraphs',
    'field_layouts',
  ];
  foreach ($fields as $field) {
    if (isset($form[$field]['widget']['add_more']['operations']['#links'])) {
      uasort($form[$field]['widget']['add_more']['operations']['#links'], function ($a, $b) {
        if (empty($a['title']['#value']) || empty($b['title']['#value'])) {
          return 1;
        }
        return strnatcmp($a['title']['#value']->render(), $b['title']['#value']->render());
      });
    }
  }
}
webdrips’s picture

For me, the dropdown list wasn't even sorting by the weights set, and I had to set my custom module weight to 12 (one higher than paragraphs) to get the following code working:

...
switch ($form_id) {
  case 'blah':
    $field = 'field_blah';
    break;
...
}

if (isset($form[$field]['widget']['add_more']['add_more_select']['#options'])) {
  natsort($form[$field]['widget']['add_more']['add_more_select']['#options']);
}
...
drupaldope’s picture

I want to fix the same issue.
Why is this issue marked as "fixed" ?
There is no patch.

jaydarnell’s picture

This issue needs to be reopened.

drupaldope’s picture

who is closing unresolved issues, especially lying by marking it as "fixed" ?

ninobrownh20 made their first commit to this issue’s fork.

ninobrownh20’s picture

You can also find your field config for the paragraph field and manually sort the paragraph names in the field target_bundles setting.

settings:
  handler: 'default:paragraph'
  handler_settings:
    target_bundles:
      accordion_section: accordion_section
      anchor: anchor
      article_listing: article_listing
      background_section_break: background_section_break
      buttons: buttons
      card_gallery: card_gallery
      card_grid: card_grid
      collection: collection

The important part is the weights under target_bundles_drag_drop

    target_bundles_drag_drop:
      accordion:
        weight: 50
        enabled: true
      accordion_section:
        weight: 51
        enabled: false
      alert:
        weight: 52
        enabled: false
mlncn’s picture

It's fair for the maintainers to say the support request here is fixed, but i agree there should be a feature request opened to make it easy to alphabetize paragraph types.

If you go to any taxonomy administration page for any vocabulary (for example /admin/structure/taxonomy/manage/example/overview ) it will have a re-orderable list of terms, and there is also a "Reset to alphabetical" button at the bottom.

Paragraph configuration pages should have the same option, probably.

(That button goes to a confirmation form, which ultimately invokes this resetWeights() method, which actually simply removes the weights— with the presumption that terms everywhere will be ordered first by weight ascending, and then alphabetically by label, so that when they are all zero the alphabetical order is used by default. That would be a great model for Paragraphs to follow with paragraph types, if possible. But again— a new issue should be created.)

anybody’s picture

totally agree with @mlncn, that would be super helpful. Maybe sorting by JS in the UI would be enough and a lot simpler?

@mlncn would you maybe like to create a follow-up for that?

emil stoianov’s picture

As ninobrownh20 said the config is exported with target_bundles_drag_drop already sorted alphabetically.
The weights actually are used to display the order in the widget
To easily achieve alphabetical sort just replace the weights with 0 for all paragraph fields

Do do that I search whole config folders for "handler: 'default:paragraph'" - not sure if this is a reliable enough to get them all...
Then for each file search with a regular expression ^(\s*weight:\s*)-?\d+
And replace with $10
After that drush config-import