Skip to content

Instantly share code, notes, and snippets.

@simesy
Created March 11, 2020 02:18
Show Gist options
  • Save simesy/ab7ccab8611ff542f2cff5ba050ad15a to your computer and use it in GitHub Desktop.
Save simesy/ab7ccab8611ff542f2cff5ba050ad15a to your computer and use it in GitHub Desktop.
/**
* Implements hook_field_widget_form_alter().
*/
function mytheme_field_widget_form_alter(&$element, $form_state, $context) {
$plugin_id = $context['widget']->getPluginId();
$paragraph_type = isset($element['#paragraph_type']) ? $element['#paragraph_type'] : FALSE;
$parent_bundle = isset($element['#bundle']) ? $element['#bundle'] : FALSE;
$hidden_fields = [];
$group_links = FALSE;
$top_label = FALSE;
if ($plugin_id == 'paragraphs' && !isset($element['preview'])) {
// All fields.
// $hidden_fields = ['field_title', 'field_image', 'field_node', 'field_link', 'field_icon', 'field_html'];
if ($paragraph_type == 'item') {
switch ($parent_bundle) {
case 'accordion':
$hidden_fields = ['field_image', 'field_node', 'field_link', 'field_icon'];
$top_label = 'Accordion item';
$group_links = FALSE;
break;
case 'cta':
$hidden_fields = ['field_title', 'field_image', 'field_icon'];
$top_label = 'Call to action';
break;
case 'links':
$hidden_fields = ['field_title', 'field_image', 'field_icon', 'field_html'];
$top_label = 'Link';
$group_links = TRUE;
break;
case 'stepper':
$hidden_fields = ['field_image', 'field_node', 'field_link', 'field_icon'];
$top_label = 'Step';
break;
case 'curated':
$hidden_fields = ['field_image', 'field_icon'];
$top_label = 'Tile';
break;
default:
break;
}
}
foreach ($hidden_fields as $hide) {
if (isset($element['subform'][$hide])) {
$element['subform'][$hide]['#access'] = FALSE;
}
}
if ($top_label) {
$element['top']['type']['label']['#markup'] = '<span class="paragraph-type-label">' . $top_label . '</span>';
}
if ($group_links) {
$isset_field_node = FALSE;
if (isset($element['subform']['field_node']['widget']['#default_value']) && count($element['subform']['field_node']['widget']['#default_value'])) {
$isset_field_node = TRUE;
}
$element['subform']['linknode'] = [
'#type' => 'vertical_tabs',
'#default_tab' => $isset_field_node ? 'edit-tab-node' : 'edit-tab-link',
];
$element['subform']['tab_node'] = [
'#type' => 'details',
'#title' => 'Content page',
'#group' => 'linknode',
'#open' => $isset_field_node ? TRUE : FALSE,
];
$element['subform']['tab_node']['field_node'] = $element['subform']['field_node'];
unset ($element['subform']['field_node']);
$element['subform']['tab_link'] = [
'#type' => 'details',
'#title' => 'Other link',
'#group' => 'linknode',
'#open' => $isset_field_node ? FALSE : TRUE,
];
$element['subform']['tab_link']['field_link'] = $element['subform']['field_link'];
unset ($element['subform']['field_link']);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment