You are here

function nodesinblock_form_node_form_alter in Nodes In Block 7

Implements hook_form_BASE_FORM_ID_alter().

File

./nodesinblock.module, line 89
Nodes in block makes it possible to add multiple nodes in one block.

Code

function nodesinblock_form_node_form_alter(&$form, $form_state) {
  $node = $form['#node'];
  $saved_types = variable_get('nodesinblock_contenttypes', array());
  if (isset($saved_types[$node->type]) && $saved_types[$node->type] != '0') {
    $a = 1;
    $i = 0;
    $block_options[-1] = t('None');
    $deltas = variable_get('nodesinblock_' . $node->type . '_block', array());
    foreach ($deltas as $key => $value) {
      if ($deltas[$a] != 0) {
        $block_name = check_plain(variable_get('nodesinblock_friendlyname_' . $i, t('Nodes in block ' . $a)));
        $type = variable_get('nodesinblock_visibility_' . $i, '1') == '1' ? t('(Pos)') : t('(Neg)');
        $block_options[$i] = $block_name . ' ' . $type;
      }
      $a++;
      $i++;
    }
    if (!empty($block_options)) {
      $node_info = array(
        'weight' => 0,
        'visibility' => '*',
        'delta' => -1,
        'render' => 'full',
      );
      if (!empty($form_state['values']) && isset($form_state['values']['nodesinblock_visibility'])) {
        $node_info['visibility'] = $form_state['values']['nodesinblock_visibility'];
        $node_info['weight'] = $form_state['values']['nodesinblock_weight'];
        $node_info['delta'] = $form_state['values']['nodesinblock_delta'];
        $node_info['render'] = $form_state['values']['nodesinblock_render'];
      }
      elseif (!empty($form['nid']['#value'])) {
        $node_info = db_query("SELECT * FROM {nodesinblock} WHERE nid = :nid", array(
          ':nid' => $form['nid']['#value'],
        ))
          ->fetchAssoc();
      }
      else {
        if (variable_get('nodesinblock_referer', FALSE) && isset($_SERVER['HTTP_REFERER'])) {
          $end = '';
          if (substr($_SERVER['HTTP_REFERER'], -1) === '/') {
            $end = '/';
          }
          $base = url(NULL, array(
            'absolute' => TRUE,
          )) . $end;
          if (strpos($_SERVER['HTTP_REFERER'], $base) === 0) {
            $node_info['visibility'] = str_replace($base, '', $_SERVER['HTTP_REFERER']);
          }
        }
        $node_info['render'] = '';
      }
      $form['nodesinblock'] = array(
        '#type' => 'fieldset',
        '#title' => check_plain(variable_get('nodesinblock_' . $node->type . '_label', t('Nodes in block'))),
        '#collapsible' => variable_get('nodesinblock_' . $node->type . '_collapsible', TRUE),
        '#collapsed' => variable_get('nodesinblock_' . $node->type . '_collapsed', TRUE),
        '#group' => 'additional_settings',
        '#attached' => array(
          'js' => array(
            drupal_get_path('module', 'nodesinblock') . '/nodesinblock-node-form.js',
          ),
        ),
        '#weight' => 40,
      );
      $nodesinblock_enable = TRUE;
      if (user_access('deny configuration on node form') && $GLOBALS['user']->uid != 1) {
        $nodesinblock_enable = FALSE;
        $form['nodesinblock']['#access'] = FALSE;
      }
      $form['nodesinblock']['nodesinblock_enable'] = array(
        '#type' => 'value',
        '#value' => $nodesinblock_enable,
      );
      $form['nodesinblock']['nodesinblock_delta'] = array(
        '#type' => 'select',
        '#title' => t('Select region'),
        '#options' => $block_options,
        '#default_value' => $node_info['delta'],
        '#description' => t('The code between the round brackets indicate the visibility setting of the block.<br />Pos = Show on only the listed pages.<br />Neg = Show on every page except the listed pages.'),
      );
      $form['nodesinblock']['nodesinblock_weight'] = array(
        '#type' => 'value',
        '#value' => $node_info['weight'],
      );
      $form['nodesinblock']['nodesinblock_render'] = array(
        '#type' => 'select',
        '#title' => t('Render node as'),
        '#options' => _nodesinblock_render_options(),
        '#default_value' => $node_info['render'],
      );
      if (variable_get('nodesinblock_' . $node->type . '_render', '0') != '0') {
        $form['nodesinblock']['nodesinblock_render']['#type'] = 'value';
        $form['nodesinblock']['nodesinblock_render']['#value'] = variable_get('nodesinblock_' . $node->type . '_render', 0);
      }
      $form['nodesinblock']['nodesinblock_visibility'] = array(
        '#title' => t('Visibility'),
        '#type' => 'textarea',
        '#description' => t('Enter one page per line as Drupal paths. The \'*\' character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog. &lt;front&gt; is the front page.'),
        '#default_value' => $node_info['visibility'],
        '#wysiwyg' => FALSE,
      );
    }
  }
}