1

I'm using the Location Taxonomize module with the Address Field module to create a vocabulary of node locations.

I want to re-create the locations from scratch, and I have a term reference field attached to the node. This term reference field contains a reference to the automatically generated Location Taxonomize vocabulary.

The problem is, after I delete the vocabulary and term reference field and then re-create them, I cannot use VBO to re-save my nodes to automatically generate the Location Taxonomize terms and fill in the term reference fields. Instead, I have to save the nodes one-by-one, but there are 30,000+ nodes.

In other words: saving nodes individually works fine, but saving nodes in bulk does nothing.

The relevant code from Location Taxonomize Address Field submodule looks like this:

function location_taxonomize_af_element_submitted($form, &$form_state) {
  $lang = 'und';
  // Act on values
  $fields = $form_state['temporary']['addressfields'];
  $items = array();
  foreach ($fields as $field) {
    $deltas = $form_state['values'][$field][$lang];
    foreach ($deltas as $delta => $values) {
      // Don't process 'add more'.
      if (is_numeric($delta)) {
        // Add the Administrative Area name, if possible
        if (isset($form[$field][$lang][$delta]['locality_block']['administrative_area']['#options'])) {
          $options = $form[$field][$lang][$delta]['locality_block']['administrative_area']['#options'];
          $values['administrative_area_name'] = $options[$values['administrative_area']];
        }
        // Add the country name
        $values['country_name'] = location_taxonomize_get_country_name($values['country']);
        $items[] = $values;
      }
    }
  }
  // taxonomize
  if (!empty($items)) {
    location_taxonomize_taxonomize($items, $form, $form_state);
  }
}

As far as I can tell, this code is added by a submit handler, so when the nodes are re-saved using VBO, the submit handler is not called (I could be wrong about this; I am in over my head in this part of Drupal). What I want to do is force this bit of code to be called with VBO-- I know how to make a Rules action in code, but for nodes, that gives me the variable $node, not $form. Is there some way to go from $node to $form to run this code? Or, is there some way to re-save a node in a bulk operation as if it were saved individually?

1

You are correct that function is attached to the actual edit form when using the field, and therefore is bypassed during a VBO node save. This allows the function to be attached to any entity type that has the field (there are other ways to do this, but this approach was almost certainly easier than other options).

You can get from a node to a form using drupal_get_form() and providing the node as the form_state:

$form = drupal_get_form('YOUR_NODE_TYPE . '_node_form', $node);

Note: I have not tested that solution in your use-case.

  • Thanks. Based on your code, I can load the $form. And I can use $node as the $form_state with drupal_get_form(). However, $node is not really $form_state because it doesn't have $form_state['values'], so is there some way to re-load $form_state as if I am editing the current version of the node? – Patrick Kenny Jul 6 '16 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.