You are here

function uniqueness_form_alter in Uniqueness 7

Same name and namespace in other branches
  1. 6 uniqueness.module \uniqueness_form_alter()

Implements hook_form_alter().

Adds the in-line widget to the node add/edit form.

File

./uniqueness.module, line 148
uniqueness.module

Code

function uniqueness_form_alter(&$form, $form_state, $form_id) {
  if (!empty($form['#node_edit_form'])) {
    $type = $form['type']['#value'];
    $op = empty($form['#node']->nid) ? UNIQUENESS_ADD_NODE : UNIQUENESS_EDIT_NODE;

    // Save the content type and operation for later use
    _uniqueness_widget_store(array(
      'type' => $type,
      'op' => $op,
    ));
    if (user_access('use uniqueness widget') && in_array($op, variable_get('uniqueness_type_' . $type, array()))) {

      // Add our javascript.
      $form['uniqueness']['#attached']['js'] = array(
        drupal_get_path('module', 'uniqueness') . '/uniqueness.js',
        array(
          'data' => array(
            'uniqueness' => _uniqueness_get_js_settings($type, $form['nid']['#value']),
          ),
          'type' => 'setting',
        ),
      );

      // Save relevant form values in a temporary store, so that we can generate
      // the list of related content right away for node previews or when
      // editing existing nodes. (The store is needed because we cannot pass
      // data directly to a block from within this function.)
      $values = array();

      // Store the node id.
      if (!empty($form['nid']['#value'])) {
        $values['nid'] = $form['nid']['#value'];
      }

      // Store the title.
      if (!empty($form['title'])) {
        $values['title'] = strip_tags($form['title']['#default_value']);
      }

      // Store the tags.
      if (!empty($values['taxonomy']['tags'][1])) {
        $values['tags'] = strip_tags($form['taxonomy']['tags'][1]['#default_value']);
      }
      _uniqueness_store($values);

      // Embed inline widget if enabled.
      if (uniqueness_widget_enabled(UNIQUENESS_WIDGET_INLINE)) {

        // Get rendered list of results.
        $count = 0;
        $content = uniqueness_widget_content($count);

        // Only add the inline uniqueness widget if it has not been defined
        // already. This makes it possible for other modules to customize the
        // form element in their own hook_form_alter() implementation.
        $form['uniqueness'] += array(
          '#type' => 'fieldset',
          '#title' => filter_xss_admin(variable_get('uniqueness_default_title', t('Related content'))),
          '#collapsible' => 1,
          '#collapsed' => $count == 0,
          '#weight' => $form['title']['#weight'] + 1,
          // Place underneath title.
          'uniqueness_type' => array(),
        );
        $form['uniqueness']['uniqueness_type'] += array(
          '#type' => 'item',
          '#title' => '',
          '#markup' => $content,
        );
      }
    }
  }
}