protected function SchemaFormBuilder::uniqueAjaxId in Migrate API 8
Same name and namespace in other branches
- 8.2 src/SchemaFormBuilder.php \Drupal\migrate_api\SchemaFormBuilder::uniqueAjaxId()
Get a unique AJAX ID for use in the form AJAX handlers.
A unique ID must be provided for a group of elements for AJAX to work. The unique ID has to be set on the element and remain exactly the same for the specific element every time the form is rebuilt, even after additional form elements have been created using the 'Add another' button.
Since form elements are being generated from a schema definition, there could be a number of similar or identical subtrees of schema defintion. Because processing is recursive, the only kind of context that is present is the subtree that is being built into a form. This makes the task of creating a unique ID for that specific element difficult.
This solution prevents conflicts, but is also not a complete solution. In the case of nested sequences, where new form elements are being AJAXed whose schema match the schema of a completely unrelated subtree, IDs will conflict. A good example is 'third_party_settings', which is scattered throughout schema definitions, making it very difficult to create a stable and unique ID. Despite issues, this implementation works for all of some complicated definitions such as 'core.entity_view_display.*.*.*'.
Parameters
array $schema: The schema which is being processed.
string $context: The form subtree currently being built.
Return value
string A unique key to use for AJAX callbacks.
1 call to SchemaFormBuilder::uniqueAjaxId()
- SchemaFormBuilder::sequenceHandler in src/
SchemaFormBuilder.php - Handle building a form when a sequence is encountered.
File
- src/
SchemaFormBuilder.php, line 250 - Contains \Drupal\migrate_api\SchemaFormBuilder.
Class
- SchemaFormBuilder
- Builds forms from schema.
Namespace
Drupal\migrate_apiCode
protected function uniqueAjaxId($schema, $context) {
// @todo, web test this and investigate a better solution.
$subtree_key = md5(serialize($schema)) . $context;
if (!isset($this->ajaxIdMap[$subtree_key])) {
$this->ajaxIdMap[$subtree_key] = 0;
}
else {
$this->ajaxIdMap[$subtree_key]++;
}
return $subtree_key . $this->ajaxIdMap[$subtree_key];
}