Skip to content
Snippets Groups Projects

Issue #3457701: MaterialIcons::getFormIconField returns null causing ajax error.

Merged Bryan Sharpe requested to merge issue/material_icons-3457701:3457701-get-element into 2.0.x
1 file
+ 15
35
Compare changes
  • Side-by-side
  • Inline
@@ -2,6 +2,7 @@
namespace Drupal\material_icons\Plugin\Field\FieldWidget;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
@@ -126,7 +127,7 @@ class MaterialIcons extends WidgetBase implements ContainerFactoryPluginInterfac
$cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
// Gets font family from $form_state, if available.
$font_family = $this->getFormStateFontFamily($form_state)
$font_family = $this->getFormStateFontFamily($form, $form_state)
??
($items[$delta]->get('family')->getValue()
??
@@ -196,61 +197,40 @@ class MaterialIcons extends WidgetBase implements ContainerFactoryPluginInterfac
* The form element to be updated.
*/
public function handleIconStyleUpdated(array &$form, FormStateInterface $form_state) {
return $this->getFormIconField($form, $form_state);
$element = $this->getElement($form, $form_state);
return $element['icon'] ?? NULL;
}
/**
* Gets the underlying field name of the triggering element.
* Gets the selected value of the font family.
*
* @param array $form
* The form where the settings form is being included in.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state of the (entire) configuration form.
*
* @return array|Null
* The form element to be updated.
*/
private function getFormIconField(array $form, FormStateInterface $form_state): array|Null {
$parents = $this->getFormStateStructure($form_state);
return !is_null($parents) ? $form[$parents[3]][$parents[2]][$parents[1]]['icon'] : NULL;
}
/**
* Gets the selected value of the font family.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state of the (entire) configuration form.
*
* @return string|Null
* This is the array structure being delivered:
*/
private function getFormStateFontFamily(FormStateInterface $form_state): string|Null {
$parents = $this->getFormStateStructure($form_state);
return !is_null($parents) ? $form_state->getValue($parents[3])[$parents[1]]['family'] : NULL;
private function getFormStateFontFamily(array &$form, FormStateInterface $form_state): string|Null {
$element = $this->getElement($form, $form_state);
return $element['family'] ?? NULL;
}
/**
* Gets the selected value of the font family.
* Gets the element.
*
* @param array $form
* The form where the settings form is being included in.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state of the (entire) configuration form.
*
* @return array|Null
* This is the array structure being delivered:
* 0 => 'family'
* 1 => [delta integer]
* 2 => 'widget'
* 3 => [original field name]
* The array structure being delivered.
*/
private function getFormStateStructure(FormStateInterface $form_state): array|Null {
$triggering_element = $form_state->getTriggeringElement();
if (empty($triggering_element)) {
return NULL;
}
$parents = array_reverse($triggering_element['#array_parents']);
return array_key_exists(1, $parents) && array_key_exists(3, $parents) ? $parents : NULL;
private function getElement(array &$form, FormStateInterface $form_state): array|Null {
$trigger = $form_state->getTriggeringElement();
return $trigger ? NestedArray::getValue($form, array_slice($trigger['#array_parents'], 0, -1)) : NULL;
}
/**
Loading