You are here

function afb_form_alter in Advanced Form Block 7

Implements hook_form_alter().

File

./afb.module, line 264
Allows administrators to create blockd of node add/edit forms.

Code

function afb_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['#node']->ajax_form)) {
    if (isset($form['#node']->nid)) {
      $type = $form['#node']->nid;
    }
    else {
      $type = $form['#node']->type;
    }
    $delta = $form['#node']->delta;
    $block_detail_array = afb_get_node_form_block_data($delta);
    $type = $block_detail_array->content_type;
    $fields_data = field_info_instances("node", $type);
    $block_detail_data = unserialize($block_detail_array->data);
    $fields = empty($block_detail_data['node_fields']) ? array() : $block_detail_data['node_fields'];
    $tabs = empty($block_detail_data['vertical_tabs']) ? array() : $block_detail_data['vertical_tabs'];
    if (isset($tabs['Publishing options']) && $tabs['Publishing options'] === 0) {
      $form['options']['status'] = array(
        '#type' => 'value',
        '#value' => 1,
      );
      $form['options']['promote'] = array(
        '#type' => 'value',
        '#value' => 1,
      );
      $form['options']['sticky'] = array(
        '#type' => 'value',
        '#value' => 0,
      );
      $form['options']['#type'] = 'container';
      $form['options']['#title'] = '';
    }
    foreach ($fields as $key => $value) {
      if ($value === 0 && isset($form['#node']->nid)) {
        $form[$key]['#access'] = FALSE;
      }
      if ($value === 0 && !isset($form['#node']->nid) && $fields_data[$key]['required'] != TRUE) {
        $form[$key]['#access'] = FALSE;
      }
    }
    $form['#after_build'][] = 'afb_after_build';
    $rand = rand();
    $form['actions']['submit'] = FALSE;
    $form['actions']['preview'] = FALSE;
    $form['actions']['delete'] = FALSE;
    $form['changed']['#default_value'] = time() + 300;
    $form['#id'] = !empty($form['#node']->form_id) ? $form['#node']->form_id : $form['#id'];
    $form['ajax-submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save Node'),
      '#weight' => 50,
      '#id' => 'ajax-submit-random-' . $rand,
      '#ajax' => array(
        'wrapper' => !empty($form['#node']->form_id) ? $form['#node']->form_id : $form['#id'],
        'callback' => 'afb_ajax_handler',
        'effect' => 'fade',
      ),
      '#submit' => array(
        1 => 'node_form_submit',
      ),
    );
  }
  if (strpos($form_id, '_node_form') !== FALSE) {

    /* Make sure we have necessary includes.
     * see the issue http://drupal.org/node/1167076 and
     * http://drupal.org/node/1167076#comment-6257338.
     * The solution based on Sun's advice.
     */
    if (empty($form_state['build_info']['files']) || !in_array('modules/node/node.pages.inc', $form_state['build_info']['files'])) {
      form_load_include($form_state, 'inc', 'node', 'node.pages');
    }
  }
}