Skip to content

Machine name children

A child of type machine_name can take its value from another child of the row. Give source the path of the label child, relative to the row. The module adds the path of the row.

$form['fields'] = [
'#type' => 'element_multiple',
'#title' => $this->t('Fields'),
'#header' => TRUE,
'#element' => [
'label' => [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#title_display' => 'invisible',
'#placeholder' => $this->t('Enter label'),
],
'name' => [
'#type' => 'machine_name',
'#title' => $this->t('Name'),
'#title_display' => 'invisible',
'#machine_name' => [
'source' => ['label'],
'exists' => [static::class, 'machineNameExists'],
],
],
],
];
public static function machineNameExists(string $value): bool {
return FALSE;
}

Type a label and the machine name of that row fills in. Until a person clicks Edit, the name stays out of sight under the label.

A row where the label My first field produced the machine name my_first_field, shown under the label with an Edit link.

  • Each row has its own source, so rows do not influence each other.
  • Use #header so that the children are columns.