You are here

function ccl_actions_form in Custom Contextual Links 8

Same name and namespace in other branches
  1. 7 ccl_actions/ccl_actions.module \ccl_actions_form()

Action link add and edit form.

TODO:

  • Add ability to trigger rules.
2 string references to 'ccl_actions_form'
CclActionsForm::getFormId in ccl_actions/src/Form/CclActionsForm.php
Returns a unique string identifying the form.
ccl_actions_menu in ccl_actions/ccl_actions.module
Implements hook_menu().

File

ccl_actions/ccl_actions.module, line 42
Implementation of core actions for CCL.

Code

function ccl_actions_form($form, &$form_state, $clid = 0) {

  // Check if we are in edit mode and load the link values.
  if ($clid) {
    $link = db_query('SELECT * FROM {ccl} WHERE clid = :clid', array(
      ':clid' => $clid,
    ))
      ->fetchObject();
    $form_state['clid'] = $clid;

    // Unserialize options.
    $link->options = unserialize($link->options);
    $node_options = $link->options['node_options'];
    if ($node_options == 'node') {
      $title = db_query('SELECT title FROM {node} WHERE nid = :nid', array(
        ':nid' => $link->options['node_id'],
      ))
        ->fetchField();
      $nid_text = $title . ' [nid:' . $link->options['node_id'] . ']';
    }
  }
  $form = array();

  // Pull in library, js and css for the form.
  $form['#attached']['library'][] = array(
    'system',
    'ui.button',
  );
  $form['#attached']['js'][] = drupal_get_path('module', 'ccl') . '/ccl.js';
  $form['#attached']['css'][] = drupal_get_path('module', 'ccl') . '/ccl.css';

  // Add an ID wrapper so that the JS and CSS from the main module take effect.
  $form['#prefix'] = '<div id="ccl-add-form">';
  $form['#suffix'] = '</div>';
  $form['action_group'] = array(
    '#type' => 'fieldset',
    '#title' => t('Node Actions'),
    '#collapsible' => TRUE,
  );
  $form['action_group']['actions_select'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Core Actions'),
    '#description' => t('Select the core actions that should become availabale.'),
    '#default_value' => $clid ? $link->options['actions_select'] : array(),
    '#options' => array(
      'publish' => t('Publish/unpublish content'),
      'sticky' => t('Make content sticky/unsticky'),
      'promote' => t('Promote/remove content from front page'),
    ),
    '#required' => TRUE,
  );
  $form['options_group'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options'),
    '#collapsible' => TRUE,
  );
  $form['options_group']['node_options'] = array(
    '#type' => 'radios',
    '#title' => t('Show link for'),
    '#description' => t('Select if this link should be displayed for all nodes, all nodes of a content type or a specific node.'),
    '#options' => array(
      'node' => t('Single node'),
      'ct' => t('Content type'),
      'global' => t('All nodes'),
    ),
    '#default_value' => isset($node_options) ? $node_options : 'node',
  );

  // Load the content type names.
  $types = node_type_get_names();
  $form['options_group']['node_type'] = array(
    '#type' => 'select',
    '#title' => t('Content Type'),
    '#description' => t('The content type this link will be displayed for.'),
    '#options' => $types,
    '#default_value' => $clid ? $link->options['node_type'] : -1,
    '#states' => array(
      'visible' => array(
        ':input[name="node_options"]' => array(
          'value' => 'ct',
        ),
      ),
    ),
  );
  $form['options_group']['node_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Node ID'),
    '#description' => t('Enter the title of the node or the id of the node this link should be added to.'),
    '#size' => 40,
    '#maxlength' => 128,
    '#default_value' => isset($nid_text) ? $nid_text : '',
    '#autocomplete_path' => 'admin/config/user-interface/ccl/autocomplete',
    '#states' => array(
      'visible' => array(
        ':input[name="node_options"]' => array(
          'value' => 'node',
        ),
      ),
    ),
  );
  $form['ccl_save_link'] = array(
    '#type' => 'submit',
    '#value' => t('Save Link'),
  );
  return $form;
}