You are here

public function SocialLinkWidget::updateLinkName in Social Link Field 8

Ajax callback function for update link dynamically.

Parameters

array $form: Form.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

\Symfony\Component\HttpFoundation\Request $request: Request.

Return value

\Drupal\Core\Ajax\AjaxResponse Ajax response.

File

src/Plugin/Field/FieldWidget/SocialLinkWidget.php, line 215

Class

SocialLinkWidget
Plugin implementation of the 'open_hours' widget.

Namespace

Drupal\social_link_field\Plugin\Field\FieldWidget

Code

public function updateLinkName(array $form, FormStateInterface $form_state, Request $request) {
  $response = new AjaxResponse();
  $element = $form_state
    ->getTriggeringElement();
  $field_name = $element['#data']['field_name'];
  $delta = $element['#data']['delta'];

  // Get field element on default field settings form.
  if (isset($form['default_value']) && $form['default_value']['widget']['#field_name'] == $field_name) {
    $element_link = $form['default_value']['widget'][$delta]['link'];
  }
  else {
    $element_link = $form[$field_name]['widget'][$delta]['link'];
  }
  if (!empty($element['#value'])) {
    $element_link['#field_prefix'] = $this->platforms[$element['#value']]['urlPrefix'];
  }
  $response
    ->addCommand(new ReplaceCommand('#' . $field_name . '-' . $delta . '-link-wrapper', $element_link));
  return $response;
}