You are here

public static function PadoAddToCartFormatter::getReferenceFieldOptions in Commerce Product Add-on 8

Get candidates for product add-on fields.

Parameters

FieldDefinitionInterface $field_definition: The field definition of the variations field we want to display add-ons for.

Return value

array All the product entity reference fields on the same product keyed by their field machine names.

2 calls to PadoAddToCartFormatter::getReferenceFieldOptions()
PadoAddToCartFormatter::isApplicable in src/Plugin/Field/FieldFormatter/PadoAddToCartFormatter.php
Returns if the formatter can be used for the provided field.
PadoAddToCartFormatter::settingsForm in src/Plugin/Field/FieldFormatter/PadoAddToCartFormatter.php
Returns a form to configure settings for the formatter.

File

src/Plugin/Field/FieldFormatter/PadoAddToCartFormatter.php, line 123

Class

PadoAddToCartFormatter
Plugin implementation of the 'commerce_pado_add_to_cart' formatter.

Namespace

Drupal\commerce_pado\Plugin\Field\FieldFormatter

Code

public static function getReferenceFieldOptions(FieldDefinitionInterface $field_definition) {
  $options = [];
  $entity_type = $field_definition
    ->getTargetEntityTypeId();
  $bundle = $field_definition
    ->getTargetBundle();
  if (empty($bundle)) {
    return $options;
  }
  $fields = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions($entity_type, $bundle);

  /** @var \Drupal\field\Entity\FieldConfig $field */
  foreach ($fields as $field) {
    if ($field
      ->getType() === 'entity_reference') {
      $field_storage_config = \Drupal\field\Entity\FieldStorageConfig::loadByName($entity_type, $field
        ->getName());
      if ($field_storage_config && $field_storage_config
        ->getSetting('target_type') == 'commerce_product') {
        $options[$field
          ->getName()] = $field
          ->getLabel() . ' (' . $field
          ->getName() . ')';
      }
    }
  }
  return $options;
}