You are here

public function ParagraphsWidget::getAllowedTypes in Paragraphs 8

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.
7 calls to ParagraphsWidget::getAllowedTypes()
ParagraphsWidget::buildAddActions in src/Plugin/Field/FieldWidget/ParagraphsWidget.php
Add 'add more' button, if not working with a programmed form.
ParagraphsWidget::buildNestedParagraphsFoDragDrop in src/Plugin/Field/FieldWidget/ParagraphsWidget.php
Builds the nested drag and drop structure.
ParagraphsWidget::getAccessibleOptions in src/Plugin/Field/FieldWidget/ParagraphsWidget.php
Returns the available paragraphs type.
ParagraphsWidget::getDefaultParagraphTypeLabelName in src/Plugin/Field/FieldWidget/ParagraphsWidget.php
Returns the default paragraph type.
ParagraphsWidget::getDefaultParagraphTypeMachineName in src/Plugin/Field/FieldWidget/ParagraphsWidget.php
Returns the machine name for default paragraph type.

... See full list

File

src/Plugin/Field/FieldWidget/ParagraphsWidget.php, line 979

Class

ParagraphsWidget
Plugin implementation of the 'entity_reference_revisions paragraphs' widget.

Namespace

Drupal\paragraphs\Plugin\Field\FieldWidget

Code

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

  /** @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] = array(
          'label' => $bundle['label'],
          'weight' => $weight,
        );
        $weight++;
      }
    }
  }
  return $return_bundles;
}