You are here

function amp_form_node_form_alter in Accelerated Mobile Pages (AMP) 7

Same name and namespace in other branches
  1. 8.3 amp.module \amp_form_node_form_alter()
  2. 8 amp.module \amp_form_node_form_alter()
  3. 8.2 amp.module \amp_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

File

./amp.module, line 342

Code

function amp_form_node_form_alter(&$form, &$form_state, $form_id) {
  $view_mode_settings = field_view_mode_settings('node', $form_state['node']->type);

  // For content type with an AMP view mode, add a "Save and view AMP" button.
  if (isset($view_mode_settings['amp']) && $view_mode_settings['amp']['custom_settings'] == TRUE) {

    // Add the buttong between "Preview" (weight of 10) and "Delete" (15).
    $form['actions']['save_view_amp'] = array(
      '#type' => 'submit',
      '#value' => t('Save and view AMP page'),
      '#submit' => array(
        'node_form_submit',
        'amp_node_enabled_form_submit',
        'amp_node_form_submit',
      ),
      '#weight' => 12,
      '#access' => TRUE,
    );

    // Add the buttong between "Preview" (weight of 10) and "Delete" (15).
    $form['actions']['save_view_amp_warnfix'] = array(
      '#type' => 'submit',
      '#value' => t('Save and view AMP page and see any AMP formatter warnings'),
      '#submit' => array(
        'node_form_submit',
        'amp_node_enabled_form_submit',
        'amp_node_form_submit_warnfix',
      ),
      '#weight' => 13,
      '#access' => TRUE,
    );

    // Option to disable AMP.
    // When adding new nodes, AMP will be turned on by default.
    $amp_enabled = 1;

    // If we are editing an existing node, get the value from database.
    if (isset($form_state['node']->nid) && !amp_db_is_node_enabled($form_state['node']->nid)) {
      $amp_enabled = 0;
    }
    $form['options']['amp_enabled'] = array(
      '#title' => t('Published (AMP)'),
      '#type' => 'checkbox',
      '#default_value' => $amp_enabled,
    );

    // Form callback for node enabled.
    $form['actions']['submit']['#submit'][] = 'amp_node_enabled_form_submit';
  }
}