0

How does one add a Location CCK field and instance programmatically to a content type in a Drupal 7 module install? I am mainly concerned with what params need to be given to the field_create_field and field_create_instance functions, assuming it is created in the same way as core fields? I've put some examples of the params I give to these functions in creating core fields (in case this helps):

An example of the params given to field_create_field:

'field_property_price' => [
    'field_name' => 'field_property_price',
    'label' => $t('Price'),
    'cardinality' => 1,
    'type' => 'text',
    'module' => 'text',
    'length' => '20'
];

An example of the params given to field_create_instance:

'field_property_price' => [
    'field_name' => 'field_property_price',
    'label' => $t('Price'),
    'cardinality' => 1,
    'required' => true,
    'widget' => [
        'weight' => '0',
        'type' => 'text_textfield',
        'module' => 'text',
        'active' => 1,
        'settings' => [
            'size' => '20',
        ],
    ],
],

Thanks for reading.

0

Never mind I figured it out. The following params work (note that the entity type and bundle params are also needed, but for my own usage I add these later).

For field_create_field:

'field_property_location' => [
    'field_name' => 'field_property_location',
    'label' => $t('Location'),
    'type' => 'location',
    'module' => 'location_cck',
    'settings' => [
        'location_settings' => [
            'form' => [
                'fields' => [
                    'name' => [
                        'collect' => 1,
                        'default' => '',
                        'weight' => 1,
                    ],
                    'street' => [
                        'collect' => 1,
                        'default' => '',
                        'weight' => 2,
                    ],
                    'additional' => [
                        'collect' => 1,
                        'default' => '',
                        'weight' => 3,
                    ],
                    'city' => [
                        'collect' => 1,
                        'default' => '',
                        'weight' => 4,
                    ],
                    'province' => [
                        'collect' => 1,
                        'widget' => 'autocomplete',
                        'default' => '',
                        'weight' => 5,
                    ],
                    'postal_code' => [
                        'collect' => 1,
                        'default' => '',
                        'weight' => 6,
                    ],
                    'country' => [
                        'collect' => 1,
                        'default' => 'gb',
                        'weight' => 7,
                    ],
                    'locpick' => [
                        'collect' => 0,
                        'weight' => 20,
                    ],
                ]
            ],
        ]
    ],
],

For field_create_instance:

'field_property_location' => [
    'field_name' => 'field_property_location',
    'label' => $t('Location'),
    'required' => true,
    'widget' => [
        'weight' => '0',
        'type' => 'location',
        'module' => 'location_cck',
        'active' => 1,
    ],
     display' => [
        'default' => [
            'label' => 'above',
            'type' => 'location_default',
            'weight' => 0,
        ],
    ],
],

There are more params you can fiddle with but these are the ones that suited my needs. To figure the params (and to figure out params for other fields like this in future), first add the fields manually to the content type with the desired settings in the Drupal backend. Then in the command line go to the projects root directory and used drush eval "print_r(field_info_fields())" to see all the fields added and drush eval "print_r(field_info_instances())" to see all the field instances added.

Mirror the data and structure from the print out to use in the field_create_field and field_create_instance functions as I've done above.

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.