You are here

private function PhotoswipeFieldFormatter::addEntityReferenceSettings in PhotoSwipe 8.2

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php \Drupal\photoswipe\Plugin\Field\FieldFormatter\PhotoswipeFieldFormatter::addEntityReferenceSettings()

Adds extra settings related when dealing with an entity reference.

Parameters

array $element: The settings form structure of this formatter.

Return value

array The modified settings form structure of this formatter.

1 call to PhotoswipeFieldFormatter::addEntityReferenceSettings()
PhotoswipeFieldFormatter::settingsForm in src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php
Returns a form to configure settings for the formatter.

File

src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php, line 155

Class

PhotoswipeFieldFormatter
Plugin implementation of the 'photoswipe_field_formatter' formatter.

Namespace

Drupal\photoswipe\Plugin\Field\FieldFormatter

Code

private function addEntityReferenceSettings(array $element) {
  if ($this->fieldDefinition
    ->getType() !== 'entity_reference') {
    return $element;
  }
  $target_type = $this->fieldDefinition
    ->getSetting('target_type');
  $target_bundles = $this->fieldDefinition
    ->getSetting('handler_settings')['target_bundles'];

  /* @var $fields FieldDefinitionInterface[] */
  $fields = [];
  foreach ($target_bundles as $bundle) {
    $fields += \Drupal::service('entity_field.manager')
      ->getFieldDefinitions($target_type, $bundle);
  }
  $fields = array_filter($fields, function (FieldDefinitionInterface $field) {
    return $field
      ->getType() === 'image' && $field
      ->getName() !== 'thumbnail';
  });
  $field_options = [];
  foreach ($fields as $name => $field) {
    $field_options[$name] = $field
      ->getName();
  }
  $element['photoswipe_reference_image_field'] = [
    '#title' => $this
      ->t('Image field of the referenced entity'),
    '#type' => 'select',
    '#default_value' => $this
      ->getSetting('photoswipe_reference_image_field'),
    '#options' => $field_options,
    '#description' => $this
      ->t('Field that contains the image to be used.'),
  ];
  return $element;
}