drupal 8 form alter code example
Example 1: drupal 8 form alter
function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if (isset($form['type']) && $form['type']['#value'] . '_node_settings' == $form_id) {
$upload_enabled_types = \Drupal::config('mymodule.settings')
->get('upload_enabled_types');
$form['workflow']['upload_' . $form['type']['#value']] = array(
'#type' => 'radios',
'#title' => t('Attachments'),
'#default_value' => in_array($form['type']['#value'], $upload_enabled_types) ? 1 : 0,
'#options' => array(
t('Disabled'),
t('Enabled'),
),
);
// Add a custom submit handler to save the array of types back to the config file.
$form['actions']['submit']['#submit'][] = 'mymodule_upload_enabled_types_submit';
}
}
Example 2: alter paragraph form drupal 8
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function paragraphs_test_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
if ($element['#paragraph_type'] == 'altered_paragraph') {
$element['subform']['field_text']['widget'][0]['#title'] = 'Altered title';
}
}