【问题标题】:Set values for default address fields in Drupal 8在 Drupal 8 中设置默认地址字段的值
【发布时间】:2019-07-23 06:20:45
【问题描述】:

创建节点时,我需要为默认地址字段(语言代码、国家代码、管理区域、地址区域等)设置值。我在由Drupal\Core\Form\FormBase 类扩展的Form 类的submitForm 函数中使用了以下代码。但这对我不起作用。

$venueNode = Node::create([
    'type'          => 'venue',
    'title'         => 'Venue',
    'field_address' => [
        'country_code'        => 'US',
        'address_line1'       => '1098 Alta Ave',
        'locality'            => 'Mountain View',
        'administrative_area' => 'US-CA',
        'postal_code'         => '94043',
    ],
]);

$venueNode->save();

【问题讨论】:

    标签: drupal-8 drupal-nodes


    【解决方案1】:

    我在这里犯了一个错误。 field_address 应该有一个0 索引。因此代码应该如下所示。

    $venueNode = Node::create([
        'type'          => 'venue',
        'title'         => 'Venue',
        'field_address' => [
            0 => [
                'country_code'        => 'US',
                'address_line1'       => '1098 Alta Ave',
                'locality'            => 'Mountain View',
                'administrative_area' => 'US-CA',
                'postal_code'         => '94043',
            ],
        ],
    ]);
    
    $venueNode->save();
    

    【讨论】:

      猜你喜欢
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2015-11-26
      • 1970-01-01
      • 2012-01-01
      相关资源
      最近更新 更多