You are here

function nodeblock_form_node_form_alter in Nodeblock 7

Implements hook_form_node_form_alter().

File

./nodeblock.module, line 246
Enables use of specified node types as custom blocks.

Code

function nodeblock_form_node_form_alter(&$form, &$form_state) {
  $node = $form['#node'];
  if (nodeblock_type_enabled($node->type) && user_access('maintain nodeblock')) {
    $overrides = nodeblock_type_node_overrides($node->type);
    $nodeblock = isset($node->nodeblock) ? $node->nodeblock : array();
    $form['nodeblock'] = array(
      '#type' => 'fieldset',
      '#title' => t('Node Block'),
      '#tree' => TRUE,
      '#group' => 'additional_settings',
      '#access' => count($overrides),
    );
    $states = array();
    if (!empty($overrides['nodeblock'])) {

      // Only show these fields when nodeblock is enabled.
      $states['visible'] = array(
        ':input[name="nodeblock[enabled]"]' => array(
          'checked' => TRUE,
        ),
      );
    }

    // Default value for enabled checkbox.
    $default_enabled = empty($overrides['nodeblock']) || nodeblock_type_node_override_block_available($node->type) == 'enabled';
    $form['nodeblock']['enabled'] = array(
      '#type' => 'checkbox',
      '#default_value' => $nodeblock ? $nodeblock['enabled'] : $default_enabled,
      '#required' => FALSE,
      '#title' => t('Create a block for this node'),
      '#description' => t('Should a block be created for this node?'),
      '#access' => !empty($overrides['nodeblock']),
    );
    $langcode = $node->language;

    // Get langcode from entity translation.
    if (module_exists('entity_translation')) {
      $handler = entity_translation_get_handler('node', $node);
      $form_langcode = $handler
        ->getFormLanguage();
      if (isset($nodeblock['custom_block_title'][$form_langcode])) {
        $langcode = $form_langcode;
      }
    }
    $form['nodeblock']['custom_block_title'] = array(
      '#type' => 'checkbox',
      '#default_value' => $nodeblock && $nodeblock['custom_block_title'][$langcode],
      '#required' => FALSE,
      '#title' => t('Provide a block title'),
      '#description' => t('Nodeblock builds a block title based on the node title. You can alter the block title here. On the block overviews the node title will be used, when displaying the node this block title will be used.'),
      '#states' => $states,
      '#access' => !empty($overrides['block_title']),
    );
    $block_title_field = nodeblock_type_node_override_block_title_field($node->type);
    $form['nodeblock']['block_title'] = array(
      '#title' => t('Provide a block title'),
      '#type' => 'textfield',
      '#maxlength' => 255,
      '#default_value' => $nodeblock ? $nodeblock['block_title'] : '',
      '#required' => FALSE,
      '#access' => !empty($overrides['block_title']) && $block_title_field == 'nodeblock',
      '#states' => array(
        'visible' => array(
          ':input[name="nodeblock[custom_block_title]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    if (!empty($overrides['nodeblock'])) {
      $form['nodeblock']['block_title']['#states']['visible'][':input[name="nodeblock[enabled]"]'] = array(
        'checked' => TRUE,
      );
    }
    if (!empty($overrides['block_title']) && $block_title_field != 'nodeblock' && !empty($overrides['block_title_in_fieldset'])) {
      $form['#pre_render'][] = 'nodeblock_node_form_pre_render';
    }
    $form['nodeblock']['custom_machine_name'] = array(
      '#type' => 'checkbox',
      '#default_value' => $nodeblock && $nodeblock['custom_machine_name'],
      '#required' => FALSE,
      '#title' => t('Provide a machine name'),
      '#description' => t("Nodeblock builds a block delta based on the node id. When you want to put a Nodeblock in a context or a panel then it'll be saved with a node id. This might provide you with problems if you are using multiple environments (e.g. a staging and production server)."),
      '#states' => $states,
      '#access' => !empty($overrides['machine_name']),
    );
    $form['nodeblock']['machine_name'] = array(
      '#type' => 'machine_name',
      '#maxlength' => 32,
      '#default_value' => $nodeblock ? $nodeblock['machine_name'] : '',
      '#required' => FALSE,
      '#machine_name' => array(
        'exists' => '_nodeblock_machine_name_exists',
      ),
      '#access' => !empty($overrides['machine_name']),
      '#states' => array(
        'visible' => array(
          ':input[name="nodeblock[custom_machine_name]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    if (!empty($overrides['nodeblock'])) {
      $form['nodeblock']['machine_name']['#states']['visible'][':input[name="nodeblock[enabled]"]'] = array(
        'checked' => TRUE,
      );
    }
    $view_modes = array();
    $view_modes['node_block_default'] = t('Default');
    $view_modes += nodeblock_get_view_modes($node->type);
    $form['nodeblock']['view_mode'] = array(
      '#type' => 'select',
      '#options' => $view_modes,
      '#title' => t('View mode'),
      '#default_value' => $nodeblock ? $nodeblock['view_mode'] : 'node_block_default',
      '#group' => 'nodeblock',
      '#states' => $states,
      '#access' => !empty($overrides['view_mode']),
    );
    $options = array(
      'node_block_default' => t('Default'),
      '0' => t('Hide'),
      '1' => t('Show'),
    );
    $form['nodeblock']['node_link'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $nodeblock ? $nodeblock['node_link'] : 'node_block_default',
      '#title' => t('Node Link Display'),
      '#group' => 'nodeblock',
      '#states' => $states,
      '#access' => !empty($overrides['node_link']),
    );
    $form['nodeblock']['comment_link'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $nodeblock ? $nodeblock['comment_link'] : 'node_block_default',
      '#title' => t('Node Comments Display'),
      '#group' => 'nodeblock',
      '#states' => $states,
      '#access' => !empty($overrides['node_link']) && module_exists('comment'),
    );

    // Add translation fallback field for nodeblock and translation
    // enabled source nodes only.
    $translation_supported = module_exists('translation') && translation_supported_type($node->type) && empty($node->translation_source) && (empty($node->tnid) || $node->tnid == 0 || $node->tnid == $node->nid);
    $form['nodeblock']['translation_fallback'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable translation fallback?'),
      '#description' => t('If checked, the source translation node will be used when a translation for the current language does not exist. If unchecked, the block will not be displayed if a matching translation does not exist.'),
      '#default_value' => $nodeblock ? $nodeblock['translation_fallback'] : '',
      '#states' => $states,
      '#access' => !empty($overrides['translation_fallback']) && $translation_supported,
    );
  }
}