public function ParagraphSelection::getSortedAllowedTypes

Returns the sorted allowed types for the field.

Return value

array A list of arrays keyed by the paragraph type machine name with the following properties.

  • label: The label of the paragraph type.
  • weight: The weight of the paragraph type.
2 calls to ParagraphSelection::getSortedAllowedTypes()
ParagraphSelection::buildEntityQuery in paragraphs/src/Plugin/EntityReferenceSelection/ParagraphSelection.php
ParagraphSelection::validateReferenceableNewEntities in paragraphs/src/Plugin/EntityReferenceSelection/ParagraphSelection.php

File

paragraphs/src/Plugin/EntityReferenceSelection/ParagraphSelection.php, line 181

Class

ParagraphSelection
Default plugin implementation of the Entity Reference Selection plugin.

Namespace

Drupal\paragraphs\Plugin\EntityReferenceSelection

Code

public function getSortedAllowedTypes() {
  $return_bundles = [];
  $bundles = $this->entityTypeBundleInfo
    ->getBundleInfo('paragraph');
  if (!empty($this->configuration['target_bundles'])) {
    if (isset($this->configuration['negate']) && $this->configuration['negate'] == '1') {
      $bundles = array_diff_key($bundles, $this->configuration['target_bundles']);
    }
    else {
      $bundles = array_intersect_key($bundles, $this->configuration['target_bundles']);
    }
  }

  // Support for the paragraphs reference type.
  if (!empty($this->configuration['target_bundles_drag_drop'])) {
    $drag_drop_settings = $this->configuration['target_bundles_drag_drop'];
    $max_weight = count($bundles);
    foreach ($drag_drop_settings as $bundle_info) {
      if (isset($bundle_info['weight']) && $bundle_info['weight'] && $bundle_info['weight'] > $max_weight) {
        $max_weight = $bundle_info['weight'];
      }
    }

    // Default weight for new items.
    $weight = $max_weight + 1;
    foreach ($bundles as $machine_name => $bundle) {
      $return_bundles[$machine_name] = [
        'label' => $bundle['label'],
        'weight' => isset($drag_drop_settings[$machine_name]['weight']) ? $drag_drop_settings[$machine_name]['weight'] : $weight,
      ];
      $weight++;
    }
  }
  else {
    $weight = 0;
    foreach ($bundles as $machine_name => $bundle) {
      $return_bundles[$machine_name] = [
        'label' => $bundle['label'],
        'weight' => $weight,
      ];
      $weight++;
    }
  }
  uasort($return_bundles, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
  return $return_bundles;
}