What would the best way be to make the cnr items display at the front of the list instead of the end? The key line is:

                  $referenced_node->{$away_field}[$referenced_node->language][] = array('nid' => $home_node->nid);

I'd like a way of controlling that so that it worked as follows:

                  if ($crazy_logic) {
                    array_unshift($referenced_node->{$away_field}[$referenced_node->language], array('nid' => $home_node->nid));
                  }
                  else {
                    $referenced_node->{$away_field}[$referenced_node->language][] = array('nid' => $home_node->nid);
                  }

The question is, what would the best way be to encapsulate this logic? :)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DamienMcKenna’s picture

Status: Active » Needs review
FileSize
1.21 KB

This allows for a variable in the format "cnr*{$away_node_type}*{$away_field}*{$home_node->type}*{$home_field}*forwards", that defaults to TRUE; if this variable is set to FALSE then new items will be added to the front of the list instead of the end.

Obviously implementing a UX to control this would be a little complex, I used it as follows:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_gallery_node_form_alter(&$form, &$form_state, $form_id) {
  // Add a control to decide if new items are added to the front or the end of
  // the gallery_items queue.
  $node = $form['#node'];
  $home_type = $node->type;
  $home_field = 'field_gallery_items';
  $others = array(
    'gallery_ad' => array(
      'field_related_gallery' => 'field_related_gallery',
    ),
    'gallery_item' => array(
      'field_related_gallery' => 'field_related_gallery',
    ),
  );
  foreach ($others as $away_node_type => $away_fields) {
    foreach ($away_fields as $away_field) {
      $var_name = 'cnr*' . $home_type . '*' . $home_field . '*' . $away_node_type . '*' . $away_field . '*forwards';
      $field_name = 'cnr_direction_' . $away_node_type . '__' . $away_field;
      $form[$field_name] = array(
        '#type' => 'checkbox',
        '#title' => t('Add new :type items to the end of the list?', array(':type' => $away_node_type)),
        '#description' => t('By unchecking this new items will be added to the front of the list instead of the end of the list.'),
        '#default_value' => variable_get($var_name, TRUE),
      );
      $form['#groups']['group_configuration']->children[] = $field_name;
      $form['#group_children'][$field_name] = 'group_configuration';
    }
  }
  $form['#submit'][] = 'sandusky_gallery_cnr_direction';
}

/**
 * Form submission callback.
 */
function mymodule_cnr_direction(&$form, &$form_state) {
  $node = $form['#node'];
  $home_type = $node->type;
  $home_field = 'field_gallery_items';
  $others = array(
    'gallery_ad' => array(
      'field_related_gallery' => 'field_related_gallery',
    ),
    'gallery_item' => array(
      'field_related_gallery' => 'field_related_gallery',
    ),
  );
  foreach ($others as $away_node_type => $away_fields) {
    foreach ($away_fields as $away_field) {
      $var_name = 'cnr*' . $home_type . '*' . $home_field . '*' . $away_node_type . '*' . $away_field . '*forwards';
      $field_name = 'cnr_direction_' . $away_node_type . '__' . $away_field;
      if (empty($form_state['values'][$field_name])) {
        variable_set($var_name, FALSE);
      }
      else {
        variable_set($var_name, TRUE);
      }
    }
  }
}
DamienMcKenna’s picture

FileSize
1.27 KB

The patch & code in #1 did't take into consideration the node ID. Doh. Replace this code as above:

  foreach ($others as $away_node_type => $away_fields) {
    foreach ($away_fields as $away_field) {
      $var_name = 'cnr*' . $home_type . '*' . $home_field . '*' . $away_node_type . '*' . $away_field . '*' . $node->nid . '*forwards';
      $field_name = 'cnr_direction_' . $away_node_type . '__' . $away_field;
      $form[$field_name] = array(
        '#type' => 'checkbox',
        '#title' => t('Add new :type items to the end of the list?', array(':type' => $away_node_type)),
        '#description' => t('By unchecking this new items will be added to the front of the list instead of the end of the list.'),
        '#default_value' => variable_get($var_name, TRUE),
      );
      $form['#groups']['group_configuration']->children[] = $field_name;
      $form['#group_children'][$field_name] = 'group_configuration';
    }
  }

and:

  foreach ($others as $away_node_type => $away_fields) {
    foreach ($away_fields as $away_field) {
      $var_name = 'cnr*' . $home_type . '*' . $home_field . '*' . $away_node_type . '*' . $away_field . '*' . $node->nid . '*forwards';
      $field_name = 'cnr_direction_' . $away_node_type . '__' . $away_field;
      if (empty($form_state['values'][$field_name])) {
        variable_set($var_name, FALSE);
      }
      else {
        variable_set($var_name, TRUE);
      }
    }
  }

The only part that changed was the $var_name variable.