You are here

public function LayoutParagraphsWidget::getAllowedTypes in Layout Paragraphs 1.0.x

Returns the sorted allowed types for a entity reference field.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: (optional) The field definition forwhich the allowed types should be returned, defaults to the current 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 LayoutParagraphsWidget::getAllowedTypes()
LayoutParagraphsWidget::formMultipleElements in src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php
Builds the main widget form array container/wrapper.
LayoutParagraphsWidget::settingsSummary in src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php
Returns a short summary for the current widget settings.

File

src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php, line 463

Class

LayoutParagraphsWidget
Entity Reference with Layout field widget.

Namespace

Drupal\layout_paragraphs\Plugin\Field\FieldWidget

Code

public function getAllowedTypes(FieldDefinitionInterface $field_definition = NULL) {
  $return_bundles = [];

  /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selection_manager */
  $selection_manager = \Drupal::service('plugin.manager.entity_reference_selection');
  $handler = $selection_manager
    ->getSelectionHandler($field_definition ?: $this->fieldDefinition);
  if ($handler instanceof ParagraphSelection) {
    $return_bundles = $handler
      ->getSortedAllowedTypes();
  }
  else {
    $bundles = \Drupal::service('entity_type.bundle.info')
      ->getBundleInfo($field_definition ? $field_definition
      ->getSetting('target_type') : $this->fieldDefinition
      ->getSetting('target_type'));
    $weight = 0;
    foreach ($bundles as $machine_name => $bundle) {
      if (empty($this
        ->getSelectionHandlerSetting('target_bundles')) || in_array($machine_name, $this
        ->getSelectionHandlerSetting('target_bundles'))) {
        $return_bundles[$machine_name] = [
          'label' => $bundle['label'],
          'weight' => $weight,
        ];
        $weight++;
      }
    }
  }
  return $return_bundles;
}