You are here

public function ProductAttributesOverview::settingsForm in Commerce Core 8.2

Returns a form to configure settings for the formatter.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the formatter. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form elements for the formatter settings.

Overrides FormatterBase::settingsForm

File

modules/product/src/Plugin/Field/FieldFormatter/ProductAttributesOverview.php, line 127

Class

ProductAttributesOverview
Plugin implementation of the 'commerce_product_attributes_overview' formatter.

Namespace

Drupal\commerce_product\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::settingsForm($form, $form_state);
  $product_type_storage = $this->entityTypeManager
    ->getStorage('commerce_product_type');
  $attribute_storage = $this->entityTypeManager
    ->getStorage('commerce_product_attribute');

  /** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_bundle */
  $product_bundle = $product_type_storage
    ->load($this->fieldDefinition
    ->getTargetBundle());
  $attribute_map = $this->attributeFieldManager
    ->getFieldMap($product_bundle
    ->getVariationTypeId());
  $used_attributes = [];
  foreach (array_column($attribute_map, 'attribute_id') as $item) {
    $attribute = $attribute_storage
      ->load($item);
    $used_attributes[$attribute
      ->id()] = $attribute
      ->label();
  }
  $view_modes = $this->entityDisplayRepository
    ->getViewModes('commerce_product_attribute_value');
  $view_mode_labels = array_map(function ($view_mode) {
    return $view_mode['label'];
  }, $view_modes);
  $form['attributes'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Display the following attributes'),
    '#default_value' => $this
      ->getSetting('attributes'),
    '#options' => $used_attributes,
  ];
  $form['view_mode'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Attribute value display mode'),
    '#default_value' => $this
      ->getSetting('view_mode'),
    '#options' => [
      'default' => $this
        ->t('Default'),
    ] + $view_mode_labels,
  ];
  return $form;
}