encodeValidatorId example



    $form['validate'] = [
      '#type' => 'container',
      '#fieldset' => 'argument_present',
    ];
    // Validator options include derivatives with :. These are sanitized for js     // and reverted on submission.     $form['validate']['type'] = [
      '#type' => 'select',
      '#title' => $this->t('Validator'),
      '#default_value' => static::encodeValidatorId($this->options['validate']['type']),
      '#states' => [
        'visible' => [
          ':input[name="options[specify_validation]"]' => ['checked' => TRUE],
        ],
      ],
    ];

    $plugins = Views::pluginManager('argument_validator')->getDefinitions();
    foreach ($plugins as $id => $info) {
      if (!empty($info['no_ui'])) {
        continue;
      }
$this->executeView($view);

    $this->assertTrue($view->argument['null']->validateArgument($this->account->id()));
    // Reset argument validation.     $view->argument['null']->argument_validated = NULL;
    // Fail for a valid numeric, but for a user that doesn't exist     $this->assertFalse($view->argument['null']->validateArgument(32));

    $form = [];
    $form_state = new FormState();
    $view->argument['null']->buildOptionsForm($form$form_state);
    $sanitized_id = ArgumentPluginBase::encodeValidatorId('entity:user');
    $this->assertTrue($form['validate']['options'][$sanitized_id]['roles']['#states']['visible'][':input[name="options[validate][options][' . $sanitized_id . '][restrict_roles]"]']['checked']);
  }

  /** * Tests the UserName argument validator. */
  public function testArgumentValidateUserName() {
    $view = Views::getView('test_view_argument_validate_username');
    $this->executeView($view);

    $this->assertTrue($view->argument['null']->validateArgument($this->account->getAccountName()));
    


  /** * {@inheritdoc} */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form$form_state);

    $entity_type_id = $this->definition['entity_type'];
    // Derivative IDs are all entity:entity_type. Sanitized for js.     // The ID is converted back on submission.     $sanitized_id = ArgumentPluginBase::encodeValidatorId($this->definition['id']);
    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);

    // If the entity has bundles, allow option to restrict to bundle(s).     if ($entity_type->hasKey('bundle')) {
      $bundle_options = [];
      foreach ($this->entityTypeBundleInfo->getBundleInfo($entity_type_id) as $bundle_id => $bundle_info) {
        $bundle_options[$bundle_id] = $bundle_info['label'];
      }

      $form['bundles'] = [
        '#title' => $entity_type->getBundleLabel() ?: $this->t('Bundles'),
        
$options['restrict_roles'] = ['default' => FALSE];
    $options['roles'] = ['default' => []];

    return $options;
  }

  /** * {@inheritdoc} */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form$form_state);
    $sanitized_id = ArgumentPluginBase::encodeValidatorId($this->definition['id']);

    $form['restrict_roles'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Restrict user based on role'),
      '#default_value' => $this->options['restrict_roles'],
    ];

    $form['roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Restrict to the selected roles'),
      '#options' => array_map(['\Drupal\Component\Utility\Html', 'escape']user_role_names(TRUE)),
      
Home | Imprint | This part of the site doesn't use cookies.