class LingotekJobId in Lingotek Translation 3.5.x
Same name and namespace in other branches
- 8.2 src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
- 4.0.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
- 3.0.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
- 3.1.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
- 3.2.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
- 3.3.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
- 3.4.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
- 3.6.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
- 3.7.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
- 3.8.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
Provides a job id render element.
Provides a form element to enter a job ID, which is validated to ensure that does not contain disallowed characters.
As the user types text into the source element, the JavaScript converts all values to lower case, and replaces any remaining disallowed characters with a replacement.
Properties:
- #job_id: An associative array containing:
- label: (optional) Text to display as label for the machine name value after the human-readable name form element. Defaults to t('Job ID').
- replace_pattern: (optional) A regular expression (without delimiters) matching disallowed characters in the machine name. Defaults to '/'.
- replace: (optional) A character to replace disallowed characters in the machine name via JavaScript. Defaults to '\' (underscore). When using a different character, 'replace_pattern' needs to be set accordingly.
- error: (optional) A custom form error message string to show, if the job id contains disallowed characters.
Usage example:
$form['id'] = array(
'#type' => 'job_id',
'#job_id' => array(
'pattern' => '[^\\/]*',
'replace_pattern' => '/',
'replace' => '\\',
),
);
Plugin annotation
@FormElement("lingotek_job_id");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\Textfield
- class \Drupal\lingotek\Element\LingotekJobId
- class \Drupal\Core\Render\Element\Textfield
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of LingotekJobId
See also
\Drupal\Core\Render\Element\Textfield
5 #type uses of LingotekJobId
- LingotekConfigManagementForm::buildForm in src/
Form/ LingotekConfigManagementForm.php - Form constructor.
- LingotekJobAssignToMultipleConfigForm::buildForm in src/
Form/ LingotekJobAssignToMultipleConfigForm.php - Form constructor.
- LingotekJobAssignToMultipleEntitiesForm::buildForm in src/
Form/ LingotekJobAssignToMultipleEntitiesForm.php - Form constructor.
- LingotekManagementForm::getFilters in src/
Form/ LingotekManagementForm.php - Gets the filters for rendering.
- LingotekManagementFormBase::getBulkOptions in src/
Form/ LingotekManagementFormBase.php - Gets the bulk options form array structure.
File
- src/
Element/ LingotekJobId.php, line 47
Namespace
Drupal\lingotek\ElementView source
class LingotekJobId extends Textfield {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#default_value' => NULL,
'#process' => [
[
$class,
'processJobId',
],
[
$class,
'processAjaxForm',
],
],
'#element_validate' => [
[
$class,
'validateJobId',
],
],
'#pre_render' => [
[
$class,
'preRenderTextfield',
],
],
'#theme' => 'input__textfield',
'#theme_wrappers' => [
'form_element',
],
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input !== FALSE && $input !== NULL) {
// This should be a string, but allow other scalars since they might be
// valid input in programmatic form submissions.
return is_scalar($input) ? (string) $input : '';
}
return NULL;
}
/**
* Processes a machine-readable name form element.
*
* @param array $element
* The form element to process. See main class documentation for properties.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
* The processed element.
*/
public static function processJobId(&$element, FormStateInterface $form_state, &$complete_form) {
// Apply default form element properties.
$element += [
'#title' => t('Job ID'),
'#description' => t('Assign a job id that you can filter on later on the TMS or in this page.'),
'#job_id' => [],
'#size' => 50,
];
// A form element that doesn't need to set any #job_id property would leave
// all properties undefined, if the defaults were defined by an element
// plugin. Therefore, we apply the defaults here.
$element['#job_id'] += [
'element' => '#' . $element['#id'],
'label' => t('Job ID'),
'pattern' => '[^\\/\\]+',
// We need an extra \ because this is consumed by the JS.
'replace_pattern' => '[\\/\\\\]+',
'replace' => '-',
];
// By default, machine names are restricted to Latin alphanumeric characters.
// So, default to LTR directionality.
if (!isset($element['#attributes'])) {
$element['#attributes'] = [];
}
$element['#attributes'] += [
'dir' => LanguageInterface::DIRECTION_LTR,
];
$element['#attached']['library'][] = 'lingotek/lingotek.job';
$options = [
'replace_pattern',
'replace',
'element',
'label',
];
$element['#attached']['drupalSettings']['lingotekJobId']['#' . $element['#id']] = array_intersect_key($element['#job_id'], array_flip($options));
return $element;
}
/**
* Form element validation handler for job_id elements.
*
* This checks that the submitted value:
* - Does not contain disallowed characters.
*/
public static function validateJobId(&$element, FormStateInterface $form_state, &$complete_form) {
// Verify that the job ID contains no disallowed characters.
if (preg_match('@' . $element['#job_id']['replace_pattern'] . '@', $element['#value'])) {
if (!isset($element['#job_id']['error'])) {
$form_state
->setError($element, t('The job ID name cannot contain invalid chars as "/" or "\\".'));
}
else {
$form_state
->setError($element, $element['#job_id']['error']);
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
LingotekJobId:: |
public | function |
Returns the element properties for this element. Overrides Textfield:: |
|
LingotekJobId:: |
public static | function | Processes a machine-readable name form element. | |
LingotekJobId:: |
public static | function | Form element validation handler for job_id elements. | |
LingotekJobId:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides Textfield:: |
|
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
Textfield:: |
public static | function | Prepares a #type 'textfield' render element for input.html.twig. |