You are here

function addanother_form_node_form_alter in Add Another 8

Implements hook_form_BASE_FORM_ID_form_alter().

File

./addanother.module, line 94
Allows users to Add Another node of the same type easily.

Code

function addanother_form_node_form_alter(&$form, FormStateInterface &$form_state) {

  /* @var \Drupal\node\NodeInterface $node  */
  if ($node = $form_state
    ->getFormObject()
    ->getEntity()) {
    if (empty($node
      ->Id())) {
      $type = $node
        ->getType();
      $config = \Drupal::config('addanother.settings');
      $account = \Drupal::currentUser();
      if ($account
        ->hasPermission('use add another')) {
        $button = $config
          ->get('button.' . $type);
        if ($button) {
          $form['actions']['addanother'] = [
            '#attributes' => [
              'class' => [
                'button',
              ],
            ],
            '#type' => 'submit',
            '#value' => t('Save and add another'),
            '#weight' => 50,
            '#submit' => $form['actions']['submit']['#submit'],
          ];
          $form['actions']['addanother']['#submit'][] = 'addanother_node_form_submit';
        }
      }
      if ($config
        ->get('message.' . $type)) {
        $form['actions']['submit']['#submit'][] = 'addanother_node_form_message_submit';
        $form['actions']['publish']['#submit'][] = 'addanother_node_form_message_submit';
        $form['actions']['unpublish']['#submit'][] = 'addanother_node_form_message_submit';
      }
    }
  }
}