You are here

function block_titlelink_form_alter in Block Title Link 7

Same name and namespace in other branches
  1. 8 block_titlelink.module \block_titlelink_form_alter()
  2. 6.2 block_titlelink.module \block_titlelink_form_alter()
  3. 6 block_titlelink.module \block_titlelink_form_alter()

Implementation of hook_form_alter

File

./block_titlelink.module, line 26
module for adding a link to a block title

Code

function block_titlelink_form_alter(&$form, &$form_state, $form_id) {
  if (($form_id == 'block_admin_configure' || $form_id == 'block_add_block_form') && user_access('set block title links')) {
    $block = new stdClass();
    $block->module = $form['module']['#value'];
    $block->delta = $form['delta']['#value'];
    $titlelink_data = _block_titlelink_get_data($block);
    $url = isset($titlelink_data['url']) ? $titlelink_data['url'] : NULL;
    $title = isset($titlelink_data['title']) ? $titlelink_data['title'] : NULL;
    $targets = drupal_map_assoc(array(
      '_blank',
      '_self',
      '_parent',
      '_top',
    ));
    $target = isset($titlelink_data['target']) ? $titlelink_data['target'] : NULL;
    $display_link = isset($titlelink_data['display']) ? $titlelink_data['display'] : TRUE;

    //Define Titlelink form elements
    $form['settings']['block_titlelink'] = array(
      '#type' => 'fieldset',
      '#title' => t('Block Title Link Settings'),
      '#collapsible' => TRUE,
      '#collapsed' => !empty($url) ? FALSE : TRUE,
      '#weight' => 0,
      '#tree' => TRUE,
    );
    $form['settings']['block_titlelink']['title_link'] = array(
      '#type' => 'textfield',
      '#title' => t('Title Path'),
      '#default_value' => $url,
      '#description' => t('URL path of Block Title. Tokens are supported. If this field is left blank, the block title link information will be removed and set back to defaults for this block.'),
      '#maxlength' => '255',
    );
    $form['settings']['block_titlelink']['title_link_title'] = array(
      '#type' => 'textfield',
      '#title' => t('title attribute'),
      '#description' => t('value for the <a> title attribute.'),
      '#default_value' => $title,
    );
    $form['settings']['block_titlelink']['title_link_target'] = array(
      '#type' => 'select',
      '#title' => t('link target'),
      '#options' => $targets,
      '#empty_value' => '',
      '#description' => t('Add a target to open the link in a new tab/window'),
      '#default_value' => $target,
    );
    $form['settings']['block_titlelink']['display_link'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display Link'),
      '#description' => t('Select this option if title should render as a link. If deselected, the title path value is stored within the block object as $block->title_link but is not rendered. This is useful if you wish to use the link elsewhere in the block template (ex: as an icon).'),
      '#default_value' => $display_link,
    );

    //Provide Token Help
    $form['settings']['block_titlelink']['token_help'] = array(
      '#theme' => 'token_tree',
      '#token_types' => array(
        'user',
      ),
    );

    //Assign weight to block title so it appears above link
    if (!isset($form['block_settings']['#weight'])) {
      $form['block_settings']['#weight'] = -1;
    }
    $form['#validate'][] = 'block_titlelink_form_validate';
    $form['#submit'][] = 'block_titlelink_form_submit';
  }
}