You are here

public function EntityReferenceLayoutRevisioned::fieldSettingsForm in Entity Reference with Layout 8

Manipulate configuration data for settings form.

Overrides EntityReferenceItem::fieldSettingsForm

File

src/Plugin/Field/FieldType/EntityReferenceLayoutRevisioned.php, line 131

Class

EntityReferenceLayoutRevisioned
Entity Reference Layout Revisioned field type.

Namespace

Drupal\entity_reference_layout\Plugin\Field\FieldType

Code

public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  $form = parent::fieldSettingsForm($form, $form_state);
  $handler_settings = $this
    ->getSetting('handler_settings');
  $target_type = $this
    ->getSetting('target_type');
  $negate = isset($handler_settings['negate']) ? $handler_settings['negate'] : 0;
  $input_negate = $form_state
    ->getValue([
    'settings',
    'handler_settings',
    'negate',
  ]);
  if (isset($input_negate)) {
    $negate = $input_negate;
  }
  $form['handler']['handler_settings']['negate']['#ajax'] = TRUE;
  $target_bundles = isset($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : [];

  // For AJAX, also look at form_state values:
  $input_target_bundles = $form_state
    ->getValue([
    'settings',
    'handler_settings',
    'target_bundles',
  ]);
  if (!empty($input_target_bundles)) {
    $target_bundles = $input_target_bundles;
  }
  $target_bundle_options = $form['handler']['handler_settings']['target_bundles']['#options'];
  if ($negate) {
    $layout_bundle_options = array_diff_key($target_bundle_options, $target_bundles);
  }
  else {
    $layout_bundle_options = array_intersect_key($target_bundle_options, $target_bundles);
  }
  if (!empty($form['handler']['handler_settings']['target_bundles_drag_drop'])) {
    foreach (Element::children($form['handler']['handler_settings']['target_bundles_drag_drop']) as $item) {
      $form['handler']['handler_settings']['target_bundles_drag_drop'][$item]['enabled']['#ajax'] = TRUE;
    }
  }
  $default_layout_bundle = '';
  if (isset($handler_settings['layout_bundles'])) {
    $default_layout_bundle = reset($handler_settings['layout_bundles']);
  }
  if (empty($layout_bundle_options[$default_layout_bundle])) {
    $default_layout_bundle = [];
  }
  $form['handler']['handler_settings']['layout_bundles'] = [
    '#type' => 'radios',
    '#options' => $layout_bundle_options,
    '#title' => $this
      ->t('Layout @target_type type', [
      '@target_type' => $target_type,
    ]),
    '#default_value' => $default_layout_bundle,
    '#multiple' => TRUE,
    '#description' => $this
      ->t('Which @target_type type should be used for layout.', [
      '@target_type' => $target_type,
    ]),
    '#required' => TRUE,
    '#id' => 'erl-layout-bundles-select',
  ];
  $layout_groups = \Drupal::service('plugin.manager.core.layout')
    ->getLayoutOptions();
  $layout_groups_defaults = isset($handler_settings['allowed_layouts']) ? $handler_settings['allowed_layouts'] : [];
  foreach ($layout_groups as $group => $layouts) {
    if (!empty($group)) {
      $defaults = isset($layout_groups_defaults[$group]) ? array_keys($layout_groups_defaults[$group]) : [];
      $form['handler']['handler_settings']['allowed_layouts'][$group] = [
        '#type' => 'checkboxes',
        '#options' => $layouts,
        '#title' => $group,
        '#multiple' => TRUE,
        '#default_value' => $defaults,
      ];
    }
  }
  $form['handler']['handler_settings']['allowed_layouts']['#prefix'] = '<b>' . $this
    ->t('Allowed Layouts:') . '</b>';
  return $form;
}