You are here

function gatsby_form_alter in Gatsby Live Preview & Incremental Builds 8

Same name and namespace in other branches
  1. 2.0.x gatsby.module \gatsby_form_alter()

Implements hook_form_alter().

File

./gatsby.module, line 35
Contains gatsby.module.

Code

function gatsby_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Add Gatsby Preview button to content type settings form.
  if ($form_id == 'node_type_edit_form') {

    // Get Preview & iFrame settings.
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    $preview_settings = $entity
      ->getThirdPartySetting('gatsby', 'preview');
    $target_settings = $entity
      ->getThirdPartySetting('gatsby', 'target');
    $iframe_settings = $entity
      ->getThirdPartySetting('gatsby', 'iframe');
    $form['gatsby'] = [
      '#title' => t('Gatsby Preview'),
      '#type' => 'details',
      '#group' => 'additional_settings',
    ];
    $form['gatsby']['gatsby_preview'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable Gatsby Preview Button'),
      '#default_value' => !empty($preview_settings),
      '#description' => t('This will add a Gatsby Preview button to node pages.'),
    ];
    $form['gatsby']['gatsby_preview_target'] = [
      '#type' => 'select',
      '#title' => t('Gatsby Preview Button Target'),
      '#options' => [
        'window' => t('New Window'),
        'sidebar' => t('Sidebar on Node Edit page'),
      ],
      '#description' => t('This controls the behavior of the Gatsby preview button.'),
      '#default_value' => !empty($target_settings) ? $target_settings : [],
      '#states' => [
        'visible' => [
          ':input[name="gatsby_preview"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['gatsby']['gatsby_iframe'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable Gatsby iFrame Preview'),
      '#default_value' => !empty($iframe_settings),
      '#description' => t('This will add an iFrame display to node pages.'),
    ];
    $form['#entity_builders'][] = 'gatsby_preview_node_entity_builder';
  }
  elseif (preg_match('/node_(\\w*)_edit_form/', $form_id, $matches)) {
    $moduleHandler = \Drupal::service('module_handler');
    if ($moduleHandler
      ->moduleExists('gatsby_endpoints')) {
      return;
    }

    // Get entity_type.
    $entity = $form_state
      ->getFormObject()
      ->getEntity();

    // Load settings.
    $entity_type = NodeType::load($entity
      ->bundle());
    $preview_settings = $entity_type
      ->getThirdPartySetting('gatsby', 'preview');
    $target_settings = $entity_type
      ->getThirdPartySetting('gatsby', 'target');
    $server_urls = array_map('trim', explode(',', \Drupal::config('gatsby.settings')
      ->get('server_url')));
    $server_url = reset($server_urls);
    if (!empty($preview_settings) && !empty($server_url)) {
      $node = \Drupal::routeMatch()
        ->getParameter('node');

      // Add Gatsby Preview button.
      $form['actions']['gatsby_preview'] = [
        '#type' => 'button',
        '#weight' => 5,
      ];
      $form['actions']['gatsby_preview']['#value'] = 'Open Gatsby Preview';
      $form['actions']['gatsby_preview']['#attributes']['class'] = [
        'gatsby-preview',
      ];

      // Implement "Open Preview" action.
      $form['actions']['gatsby_preview']['#attached'] = [
        'drupalSettings' => [
          'gatsby_preview_url' => $server_url,
          'gatsby_path' => \Drupal::service('gatsby.path_mapping')
            ->getPath($node),
          'gatsby_preview_target' => !empty($target_settings) ? $target_settings : 'window',
        ],
        'library' => [
          'gatsby/open_preview',
        ],
      ];
    }
  }
}