You are here

public function EntityViewDisplayAlterer::addSmartTitle in Smart Title 8

Adds Smart Title to the entity form.

Parameters

array $form: The renderable array of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state object.

File

src/EntityViewDisplayAlterer.php, line 55

Class

EntityViewDisplayAlterer
Entity view display form alterer class for Smart Title.

Namespace

Drupal\smart_title

Code

public function addSmartTitle(array &$form, FormStateInterface $form_state) {
  if (!($entity = static::getViewDisplayEntityFromFormState($form_state))) {
    return;
  }
  if ($entity
    ->getThirdPartySetting('layout_builder', 'enabled')) {
    return;
  }
  $smart_title_config = $this->configFactory
    ->get('smart_title.settings')
    ->get('smart_title');
  $target_entity_type_id = $entity
    ->getTargetEntityTypeId();
  $target_entity_bundle = $entity
    ->getTargetBundle();

  // Add Smart Title checkbox to the entity view display form.
  if ($smart_title_config && in_array("{$target_entity_type_id}:{$target_entity_bundle}", $smart_title_config)) {
    $this
      ->addSmartTitleBuilder($form, $form_state);
  }
  else {
    return;
  }

  // Hide the extra field if smart title isn't used on this view display.
  if (!$entity
    ->getThirdPartySetting('smart_title', 'enabled', FALSE)) {
    unset($form['#extra'][array_search('smart_title', $form['#extra'])]);
    unset($form['fields']['smart_title']);
    return;
  }
  $provide_form = !empty($form_state
    ->getStorage()['plugin_settings_edit']) && $form_state
    ->getStorage()['plugin_settings_edit'] === 'smart_title';
  $smart_title =& $form['fields']['smart_title'];
  $smart_title['plugin']['settings_edit_form'] = [];
  if ($smart_title['region']['#default_value'] !== 'hidden') {

    // Extra field is set to be visible.
    // Getting our settings: the active config, or if we have temporary
    // then those.
    $smart_title_settings = $form_state
      ->get('smart_title_tempvalues') ?: $entity
      ->getThirdPartySetting('smart_title', 'settings', _smart_title_defaults($entity
      ->getTargetEntityTypeId(), TRUE));
    if ($provide_form) {
      unset($smart_title['settings_summary']);
      unset($smart_title['settings_edit']);
      $smart_title['#attributes']['class'][] = 'field-plugin-settings-editing';
      $smart_title['plugin']['#cell_attributes'] = [
        'colspan' => 3,
      ];
      $smart_title['plugin']['settings_edit_form'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'field-plugin-settings-edit-form',
          ],
        ],
        '#parents' => [
          'fields',
          'smart_title',
          'settings_edit_form',
        ],
        'label' => [
          '#markup' => $this
            ->t('Format settings:'),
        ],
        'settings' => $this
          ->getSettingsInputsFromSettings($smart_title_settings, $entity),
        'third_party_settings' => [],
        'actions' => [
          '#type' => 'actions',
          'save_settings' => [
            '#submit' => [
              [
                get_class($this),
                'multistepSubmit',
              ],
              '::multistepSubmit',
            ],
            '#ajax' => [
              'callback' => '::multistepAjax',
              'wrapper' => 'field-display-overview-wrapper',
              'effect' => 'fade',
            ],
            '#field_name' => 'smart_title',
            '#type' => 'submit',
            '#button_type' => 'primary',
            '#name' => 'smart_title_plugin_settings_update',
            '#value' => $this
              ->t('Update'),
            '#op' => 'update',
          ],
          'cancel_settings' => [
            '#submit' => [
              [
                get_class($this),
                'multistepSubmit',
              ],
              '::multistepSubmit',
            ],
            '#ajax' => [
              'callback' => '::multistepAjax',
              'wrapper' => 'field-display-overview-wrapper',
              'effect' => 'fade',
            ],
            '#field_name' => 'smart_title',
            '#type' => 'submit',
            '#name' => 'smart_title_plugin_settings_cancel',
            '#value' => $this
              ->t('Cancel'),
            '#op' => 'cancel',
            '#limit_validation_errors' => [
              [
                'fields',
                'smart_title',
                'type',
              ],
            ],
          ],
        ],
      ];
      $smart_title['plugin']['settings_edit_form']['label']['#markup'] .= ' <span class="plugin-name">Smart Title</span>';
    }
    if (!$provide_form) {
      $smart_title['settings_summary'] = [
        '#type' => 'inline_template',
        '#template' => '<div class="field-plugin-summary">{{ summary|safe_join("<br />") }}</div>',
        '#context' => [
          'summary' => $this
            ->getSummaryFromSettings($smart_title_settings, $entity),
        ],
        '#cell_attributes' => [
          'class' => [
            'field-plugin-summary-cell',
          ],
        ],
      ];
      $smart_title['settings_edit'] = [
        '#submit' => [
          [
            get_class($this),
            'multistepSubmit',
          ],
          '::multistepSubmit',
        ],
        '#ajax' => [
          'callback' => '::multistepAjax',
          'wrapper' => 'field-display-overview-wrapper',
          'effect' => 'fade',
        ],
        '#field_name' => 'smart_title',
        '#type' => 'image_button',
        '#name' => 'smart_title_settings_edit',
        '#src' => 'core/misc/icons/787878/cog.svg',
        '#attributes' => [
          'class' => [
            'field-plugin-settings-edit',
          ],
          'alt' => $this
            ->t('Edit'),
        ],
        '#op' => 'edit',
        '#limit_validation_errors' => [
          [
            'fields',
            'smart_title',
            'type',
          ],
        ],
        '#prefix' => '<div class="field-plugin-settings-edit-wrapper">',
        '#suffix' => '</div>',
      ];
    }
  }

  // Add smart title form submit handler.
  if (!empty($form['actions']) && is_array($form['actions'])) {
    $element_keys = Element::children($form['actions']);
    foreach ($element_keys as $element_key) {
      if (!isset($form['actions'][$element_key]['#type']) || $form['actions'][$element_key]['#type'] !== 'submit') {
        continue;
      }
      $submit_callbacks = isset($form['actions'][$element_key]['#submit']) ? $form['actions'][$element_key]['#submit'] : [];
      array_unshift($submit_callbacks, [
        get_class($this),
        'submitSmartTitleForm',
      ]);
      $form['actions'][$element_key]['#submit'] = $submit_callbacks;
    }
  }
  $submit_callbacks = isset($form['#submit']) ? $form['#submit'] : [];
  array_unshift($submit_callbacks, [
    get_class($this),
    'submitSmartTitleForm',
  ]);
  $form['#submit'] = $submit_callbacks;
}