protected function buttonWasClicked($element, FormStateInterface &
$form_state) { // First detect normal 'vanilla' button clicks. Traditionally, all standard
// buttons on a form share the same name (usually 'op'), and the specific
// return value is used to determine which was clicked. This ONLY works as
// long as $form['#name'] puts the value at the top level of the tree of
// \Drupal::request()->request data.
$input =
$form_state->
getUserInput();
// The input value attribute is treated as CDATA by browsers. This means
// that they replace character entities with characters. Therefore, we need
// to decode the value in $element['#value']. For more details see
// http://www.w3.org/TR/html401/types.html#type-cdata.
if (isset($input[$element['#name'
]]) &&
$input[$element['#name'
]] == Html::
decodeEntities($element['#value'
])) { return TRUE;
} // When image buttons are clicked, browsers do NOT pass the form element
// value in \Drupal::request()->Request. Instead they pass an integer
// representing the coordinates of the click on the button image. This means
// that image buttons MUST have unique $form['#name'] values, but the
// details of their \Drupal::request()->request data should be ignored.
elseif (!
empty($element['#has_garbage_value'
]) &&
isset($element['#value'
]) &&
$element['#value'
] !== ''
) { return TRUE;
} return FALSE;
}