You are here

class LingotekJobId in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 8.2 src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
  2. 4.0.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
  3. 3.0.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
  4. 3.1.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
  5. 3.2.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
  6. 3.3.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
  7. 3.5.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
  8. 3.6.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
  9. 3.7.x src/Element/LingotekJobId.php \Drupal\lingotek\Element\LingotekJobId
  10. 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

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\Element
View 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
LingotekJobId::getInfo public function Returns the element properties for this element. Overrides Textfield::getInfo
LingotekJobId::processJobId public static function Processes a machine-readable name form element.
LingotekJobId::validateJobId public static function Form element validation handler for job_id elements.
LingotekJobId::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides Textfield::valueCallback
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
Textfield::preRenderTextfield public static function Prepares a #type 'textfield' render element for input.html.twig.