You are here

function commons_bw_partial_node_form in Drupal Commons 7.3

Partial node form for the browsing widget.

1 string reference to 'commons_bw_partial_node_form'
commons_bw_forms in modules/commons/commons_bw/commons_bw.module
Implements hook_forms().

File

modules/commons/commons_bw/commons_bw.module, line 139

Code

function commons_bw_partial_node_form($form, &$form_state, $bundle, $group_id = NULL) {
  global $user;
  global $language;
  if (!$group_id) {

    // Reset the og_field_widget_form cache because otherwise it ignores
    // multiple tries to render the same group audience widget (We have the
    // same group audience widget on the All and Posts tabs, when displaying
    // this form without group context).
    drupal_static_reset('og_field_widget_form');
  }
  if ($group_id) {
    $form_state['group_id'] = $group_id;
  }
  $instances = field_info_instances('node', $bundle);

  // Remove all fields except those marked as "display_in_partial_form".
  foreach ($instances as $field_name => $instance) {
    if (empty($instance['display_in_partial_form'])) {
      unset($instances[$field_name]);
    }
  }

  // Make sure there's a field left to display.
  if (empty($instances)) {
    return $form;
  }

  // Create a dummy node for field_attach_form().
  $node = new stdClass();
  $node->type = $bundle;
  node_object_prepare($node);
  if (module_exists('locale')) {
    if (locale_multilingual_node_type($node->type)) {
      $node->language = $language->language;
    }
    else {
      $default = language_default();
      $node->language = $default->language;
    }
  }
  else {
    $node->language = LANGUAGE_NONE;
  }
  field_attach_form('node', $node, $form, $form_state, entity_language('node', $node));
  foreach (element_children($form) as $field_name) {
    if (empty($instances[$field_name])) {
      $form[$field_name]['#access'] = FALSE;
    }
  }
  if (!empty($form['#metatags'])) {
    unset($form['#metatags']);
  }

  // When not in a group context, enable the group audience widget.
  $form[OG_AUDIENCE_FIELD]['#weight'] = 100;
  $form[OG_AUDIENCE_FIELD]['#access'] = !$group_id;

  // Add a default form title.
  $form['title'] = array(
    '#markup' => t('Create content'),
    '#weight' => -100,
  );

  // Display the user's picture.
  $wrapper = entity_metadata_wrapper('user', $user);
  $path = empty($user->picture) ? variable_get('user_picture_default') : $wrapper
    ->value()->picture->uri;
  $form['user_picture'] = array(
    '#theme' => 'image_style',
    '#style_name' => '50x50_avatar',
    '#path' => $path,
    '#prefix' => '<div class="user-picture">',
    '#suffix' => '</div>',
    '#weight' => -20,
  );
  $form['actions'] = array(
    '#type' => 'actions',
    '#weight' => 200,
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  // Attach the browsing widget JS and give it a higher weight than
  // quicktabs.js.
  $form['#attached']['js'][] = array(
    'data' => drupal_get_path('module', 'commons_bw') . '/js/partial_node_form.js',
    'type' => 'file',
    'weight' => 100,
  );

  // Add in some descriptive classes for css down the line.
  $form['#attributes']['class'][] = 'node';
  $form['#attributes']['class'][] = 'commons-bw-partial-node-form';
  $form['#attributes']['class'][] = 'commons-bw-partial-node-form-' . $bundle;

  // Add a link to the full node form.
  $form['full_form'] = array(
    '#theme' => 'link',
    '#text' => t('Go to full form'),
    '#path' => 'node/add/' . str_replace('_', '-', $bundle),
    '#options' => array(
      'attributes' => array(
        'class' => array(
          'full-form',
        ),
      ),
      'html' => FALSE,
    ),
    '#weight' => 100,
  );
  if ($group_id) {
    $form['full_form']['#options']['query'] = array(
      OG_AUDIENCE_FIELD => $group_id,
    );
  }

  // Add the commons_bw after build first, in case other pre-renders needs need
  // to address fields by there CSS ID.
  array_unshift($form['#pre_render'], 'commons_bw_partial_node_form_after_build');
  $form['#validate'][] = 'commons_bw_partial_node_form_validate';
  return $form;
}