You are here

public function EntityField::formatterSettingsProcessCallback in Chaos Tool Suite (ctools) 8.3

Render API callback: builds the formatter settings elements.

File

modules/ctools_block/src/Plugin/Block/EntityField.php, line 249

Class

EntityField
Provides a block to a field on an entity.

Namespace

Drupal\ctools_block\Plugin\Block

Code

public function formatterSettingsProcessCallback(array &$element, FormStateInterface $form_state, array &$complete_form) {
  $config = $this
    ->getConfiguration();
  $parents_base = $element['#parents'];
  $formatter_parent = array_merge($parents_base, [
    'formatter_type',
  ]);
  $formatter_settings_parent = array_merge($parents_base, [
    'formatter_settings',
  ]);
  $settings_element =& $element['formatter_settings_wrapper']['formatter_settings'];

  // Set the #parents on the formatter_settings so they end up as a peer to
  // formatter_type.
  $settings_element['#parents'] = $formatter_settings_parent;

  // Get the formatter name in a way that works regardless of whether we're
  // getting the value via AJAX or not.
  $formatter_name = NestedArray::getValue($form_state
    ->getUserInput(), $formatter_parent) ?: $element['formatter_type']['#default_value'];

  // Place the formatter settings on the form if a formatter is selected.
  $formatter = $this
    ->getFormatter($formatter_name, $form_state
    ->getValue('formatter_label'), $form_state
    ->getValue($formatter_settings_parent, $config['formatter']['settings']), $config['formatter']['third_party_settings']);
  $settings_element = array_merge($formatter
    ->settingsForm($settings_element, $form_state), $settings_element);

  // Store the array parents for our element so that we can use it to pull out
  // the formatter settings in our AJAX callback.
  $complete_form['#formatter_array_parents'] = $element['#array_parents'];
  return $element;
}