I'm trying to create a node with an addressfield, and to do so I needed to add the following to Drupal7::expandEntityFields().

elseif ('addressfield' === $info['module']) {
  foreach ($value as $key => $data) {
    $new_entity->{$param}[LANGUAGE_NONE][0][$key] = $data;
  }
}

This way I can pass in

      $node->field_address = array(
        'organisation_name' => 'TestCorp',
        'country' => 'AU',
        'thoroughfare' => '1 St George Tce',
        'premise' => 'Perth',
        'locality' => 'Perth',
        'administrative_area' => 'WA',
        'postal_code' => '6000',
      );

and have it save the field.

I realise this area of the code will be refactored at some point, so don't forget poor old addressfield!.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhedstrom’s picture

If you set the data structure

      $node->field_address[LANGUAGE_NONE][0] = array(
        'organisation_name' => 'TestCorp',
        'country' => 'AU',
        'thoroughfare' => '1 St George Tce',
        'premise' => 'Perth',
        'locality' => 'Perth',
        'administrative_area' => 'WA',
        'postal_code' => '6000',
      );

in your FeatureContext does it work?

samhassell’s picture

That was the first thing I tried, as that is how you would do it for node_save(). expandEntityFields() doesn't seem to like this, it just wants a string, so you can do stuff like

      $node->field_summary = Random::string(128);

Then there is special handing for dates, you pass

      $node->field_date = '2014-12-10 05:45:00,2014-12-15 08:00:00';

for them.

Here's the code controlling this:

                // Special handling for date fields (start/end).
                // @todo generalize this
                if ('date' === $info['module']) {
                  // Dates passed in separated by a comma are start/end dates.
                  $dates = explode(',', $value);
                  $value = trim($dates[0]);
                  if (!empty($dates[1])) {
                    $column2 = array_shift($column_names);
                    $new_entity->{$param}[LANGUAGE_NONE][0][$column2] = trim($dates[1]);
                  }
                  $new_entity->{$param}[LANGUAGE_NONE][0][$column] = $value;
                }
                // Special handling for term references.
                elseif ('taxonomy' === $info['module']) {
                  $terms = explode(',', $value);
                  $i = 0;
                  foreach ($terms as $term) {
                    $tid = taxonomy_get_term_by_name($term);
                    if (!$tid) {
                      throw new \Exception(sprintf("No term '%s' exists.", $term));
                    }

                    $new_entity->{$param}[LANGUAGE_NONE][$i][$column] = array_shift($tid)->tid;
                    $i++;
                  }
                }
                elseif ('addressfield' === $info['module']) {
                  foreach ($value as $key => $data) {
                    $new_entity->{$param}[LANGUAGE_NONE][0][$key] = $data;
                  }
                }
                else {
                  $new_entity->{$param}[LANGUAGE_NONE][0][$column] = $value;
                }

So you can see that it builds the [LANGUAGE_NONE][$i] for us, but doesn't consider fields with more than one element in their array.

samhassell’s picture

Title: Creating Addressfields » Add handling for fields with arrays (was: Creating Addressfields)
Status: Active » Needs review
FileSize
671 bytes

Here's a more generic version of the above patch.

Still can't see how this is supported without the patch.

Cheers.

jhedstrom’s picture

Status: Needs review » Fixed

Committed in 7b91493. Thanks!

samhassell’s picture

Thanks for the named commit!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

  • Commit 7b91493 on master, 1.0 authored by samhassell, committed by jhedstrom:
    Issue #2111271 by samhassell: Added handling for fields with arrays (was...

  • Commit 7b91493 on master, 1.0, test-symfony authored by samhassell, committed by jhedstrom:
    Issue #2111271 by samhassell: Added handling for fields with arrays (was...