Dependent Dropdown in Field Widget I want to implement a Dependent Dropdown for a custom field widget.

I have a custom field with 5 select boxes. Each subsequent dropdown should populate based on values from previous Dropdown. I have a 5 functions that takes arguments to get the values.

I have implemented that, but I need to Ajaxify it, so that I can populate without page refreshes. Any pointers on how to do that?

Basically, I am trying to implement the below thing (from examples module) with FieldWidget.

https://cgit.drupalcode.org/examples/tree/ajax_example/src/Form/DependentDropdown.php
You didn't deserve the downvote: this is a perfectly good question, one that has simply no answer anywhere on the web yet. All of them only speak about dependent dropdowns in forms, not custom fields where we have two extra problems: first, being out of real form context when setting up ourselves, second, potentially being multivalued.
It took me some time to figure it out but here it is. We're extending a FieldItemBase:
public function formElement(FieldItemListInterface $items, $delta, array $element, array &amp;$form, FormStateInterface $form_state) {
  $id = $this-&gt;fieldDefinition-&gt;getUniqueIdentifier();
  $wrapper_id = &quot;XXX-$id-$delta-replace&quot;;

  $selected_value1 = $items[$delta]-&gt;value1 ?? 0;
  $element['value1'] = $element + [
    '#type' =&gt; 'select',
    '#options' =&gt; $value1_options,
    '#empty_option' =&gt; '---',
    '#default_value' =&gt; $selected_value1,
    '#weight' =&gt; 1,
    '#ajax' =&gt; [
      'event' =&gt; 'change',
      'method' =&gt; 'html',
      'callback' =&gt; [$this, 'getValues'],
      'wrapper' =&gt; $wrapper_id,
      'progress' =&gt; [
        'type' =&gt; 'throbber',
        'message' =&gt; NULL,
      ],
    ],
  ];

  $selected_value2 = $items[$delta]-&gt;value2 ?? 0;
  $element['value2'] = [
    '#type' =&gt; 'select',
    '#empty_option' =&gt; '---',
    '#default_value' =&gt; $selected_value2,
    '#options' =&gt; $this-&gt;loadOptions($selected_value1),
    '#prefix' =&gt; &quot;&lt;div id='{$wrapper_id}'&gt;&quot;,
    '#suffix' =&gt; '&lt;/div&gt;',
    '#validated' =&gt; TRUE,
    '#weight' =&gt; 2,
  ];

  $element += [
    '#type' =&gt; 'container',
    '#attributes' =&gt; ['class' =&gt; ['container-inline']],
  ];

  return $element;
}

Fill value1_options with the options of your first dropdown. Change wrapper_id as you please but we don't know the actual ID the select will receive, so we have to make sure we make it unique in some other way (note that there might be more than one of the same custom field type in the final form, so using the name and the delta only will not suffice).
Using '#validated' =&gt; TRUE is usually frowned upon but this is the only easy way to make sure the second select will not result in an &quot;Illegal choice&quot; validation error after we replace the options without the form being aware of the possible values. One standard advice is to pre-fill the second dropdown with the union of all possible values, making sure that later dependent lists always use a subset of that full list but this wasn't really feasible in my case (or any other really dynamic case). Dependent lists in custom forms can simply return the original form element, so this doesn't apply to them, but custom fields can't locate the actual corresponding $form[] element, so this is the best I could come up with. It works.
Next, the function that loads the options of the dependent dropdown, depending on the value selected in the master one:
private function loadOptions($id) {
  $options = [];
  if ($id) {
    // load whatever options belong to the value of $id
    $options = ...;
  }
  return $options;
}

And finally, the Ajax glue to hold it together:
public function getValues(array &amp;$form, FormStateInterface $form_state) {
  $triggeringElement = $form_state-&gt;getTriggeringElement();
  $selected_value = $triggeringElement['#value'];
  $html = '';
  $options = $this-&gt;loadOptions($selected_value);
  foreach ($options as $key =&gt; $value) {
    $html .= &quot;&lt;option value='$key'&gt;$value&lt;/option&gt;&quot;;
  }

  $ajax_response = new AjaxResponse();
  $wrapper_id = $triggeringElement['#ajax']['wrapper'];
  $ajax_response-&gt;addCommand(new HtmlCommand(&quot;#{$wrapper_id} select&quot;, $html));
  return $ajax_response;
}

If you compare the whole lot to the custom form solution, it's mostly the same but there are small differences in how to identify the correct element and how to actually replace the options of the dependent list that make this work in the field case.