I created an Address custom field type, with two text fields (street and city).
When I add a new Address field on a content type, I get this form inside the edit page.
How can I change the layout of the form above?
For example, I would like to add some space between the Street text line and the City label, or display both fields on the same line.
This is the code for the AddressDefaultWidget
class.
/**
* Field type widget implementation.
*
* @FieldWidget(
* id = "AddressDefaultWidget",
* label = @Translation("Address select"),
* field_types = {
* "Address"
* }
* )
*/
class AddressDefaultWidget extends WidgetBase {
/**
* Define the form for the field type.
*/
public function formElement(
FieldItemListInterface $items,
$delta,
Array $element,
Array &$form,
FormStateInterface $formState
) {
// Street
$element['street'] = [
'#type' => 'textfield',
'#title' => t('Street'),
'#default_value' => isset($items[$delta]->street) ?
$items[$delta]->street : null,
'#empty_value' => '',
'#placeholder' => t('Street'),
];
// City
$element['city'] = [
'#title' => t('City'),
'#type' => 'textfield',
'#default_value' => isset($items[$delta]->city) ?
$items[$delta]->city : null,
'#empty_value' => '',
'#placeholder' => t('City'),
];
return $element;
}
}