- Issue created by @jwilson3
- πͺπ¨Ecuador jwilson3
After trying several things, this was my last attempt before giving up and writing up this ticket.
I don't know if this was on the right path or not, but we ran out of time and budget to keep pushing on this at the end of the sprint.
THIS CODE DOES NOT WORK.
<?php use \Drupal\Core\Form\FormStateInterface; /** * @file * Primary module hooks for My Module module. */ /** * Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter * * @param array $element * @param FormStateInterface $form_state * @param array $context * @return void */ function MY_MODULE_field_widget_single_element_chart_config_default_form_alter(array &$element, FormStateInterface $form_state, array $context) { $element['#after_build'][] = 'MY_MODULE_field_element_after_build'; } /** * Processes the settings element. * * @param array $element * The form element. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. * * @return array * The element. * * @throws \Drupal\Component\Plugin\Exception\PluginException */ function MY_MODULE_field_element_after_build(array $element, FormStateInterface $form_state) { if ( !empty($element['config']['type']['#options']) ) { $valid_chart_types = ['donut', 'pie']; if ($default_type = $element['config']['#default_value']['type']) { $valid_chart_types = array_unique(array_merge($valid_chart_types, [$default_type])); } if($current_type = $element['config']['type']['#value']) { $valid_chart_types = array_unique(array_merge($valid_chart_types, [$current_type] )); } foreach ($element['config']['type']['#options'] as $key => $value) { if (!in_array($key, $valid_chart_types)) { unset($element['config']['type']['#options'][$key]); } } $form_state->setRebuild(TRUE); } return $element; }
- πΊπΈUnited States andileco
Thanks for sharing your work! We'll take a look.
- First commit to issue fork.
- Merge request !89Issue #3426704: Ability to disable some chart types β (Merged) created by andileco
-
andileco β
committed 3b1e2deb on 5.0.x
Issue #3426704 by andileco, nikathone, jwilson3: Ability to disable some...
-
andileco β
committed 3b1e2deb on 5.0.x
- Status changed to Fixed
8 months ago 8:54pm 24 April 2024 - πΊπΈUnited States andileco
This is fixed. In this situation, you would use code like this in MYMODULE.module:
function MYMODULE_charts_plugin_supported_chart_types_alter(array &$types, string $chart_plugin_id) { if ($chart_plugin_id === 'MYLIBRARY') { $types = array_diff($types, ['area', 'bar', 'bubble', 'column', 'gauge', 'line', 'scatter', 'solidgauge', 'spline',]); } }
This new hook, which is added to charts.api.php, could also allow you to add additional chart types.
function MYMODULE_charts_plugin_supported_chart_types_alter(array &$types, string $chart_plugin_id) { if ($chart_plugin_id === 'MYLIBRARY') { $types[] = 'candlestick'; } }
- πͺπ¨Ecuador jwilson3
This is fantastic. Thank you so much for your responsiveness here and for introducing the new hook as a solution to alter the charts.
Unfortunately there were other changes we were hoping to make to the edit forms as well. It seems like it's still impossible to alter the form presentation to do things like collapse the chart details element wrapper when data is already provided.
Automatically closed - issue fixed for 2 weeks with no activity.