cleanCssIdentifier example

else {
        // On the views.ajax route, set the action to the page we were on.         $form_action = Url::fromUserInput($this->currentPathStack->getPath())->toString();
      }
    }
    else {
      $form_action = $view->getUrl()->toString();
    }

    $form['#action'] = $form_action;
    $form['#theme'] = $view->buildThemeFunctions('views_exposed_form');
    $form['#id'] = Html::cleanCssIdentifier('views_exposed_form-' . $view->storage->id() . '-' . $display['id']);
    // Labels are built too late for inline form errors to work, resulting     // in duplicated messages.     $form['#disable_inline_form_errors'] = TRUE;

    /** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form_plugin */
    $exposed_form_plugin = $view->display_handler->getPlugin('exposed_form');
    $exposed_form_plugin->exposedFormAlter($form$form_state);

    // Save the form.     $this->exposedFormCache->setForm($view->storage->id()$view->current_display, $form);

    
    $options = $this->xpath('//select[@name="field_map[attribute_1]"]/option');
    $this->assertGreaterThanOrEqual(3, count($options));
    $this->assertSame('- Skip field -', $options[0]->getText());
    $this->assertSame('Name', $options[1]->getText());
    $this->assertSame('Test source', $options[2]->getText());

    // Open up the media add form and verify that the source field is right     // after the name, and before the vertical tabs.     $this->drupalGet("/media/add/$mediaTypeMachineName");

    // Get the form element, and its HTML representation.     $form_selector = '#media-' . Html::cleanCssIdentifier($mediaTypeMachineName) . '-add-form';
    $form = $assert_session->elementExists('css', $form_selector);
    $form_html = $form->getOuterHtml();

    // The name field should come before the source field, which should itself     // come before the vertical tabs.     $name_field = $assert_session->fieldExists('Name', $form)->getOuterHtml();
    $test_source_field = $assert_session->fieldExists('Test source', $form)->getOuterHtml();
    $vertical_tabs = $assert_session->elementExists('css', '.vertical-tabs', $form)->getOuterHtml();
    $date_field = $assert_session->fieldExists('Date', $form)->getOuterHtml();
    $published_checkbox = $assert_session->fieldExists('Published', $form)->getOuterHtml();
    $this->assertGreaterThan(strpos($form_html$name_field)strpos($form_html$test_source_field));
    
$role->grantPermission('administer media display');
    $role->save();
    $this->adminUser->addRole($role->id());
    $this->adminUser->save();
    $assert_session = $this->assertSession();
    $page = $this->getSession()->getPage();
    /** @var \Drupal\media\Entity\MediaType $media_type */
    foreach (MediaType::loadMultiple() as $media_type) {
      $media_type_machine_name = $media_type->id();
      $this->drupalGet('media/add/' . $media_type_machine_name);
      // Get the form element, and its HTML representation.       $form_selector = '#media-' . Html::cleanCssIdentifier($media_type_machine_name) . '-add-form';
      $form = $assert_session->elementExists('css', $form_selector);
      $form_html = $form->getOuterHtml();

      // The name field should be hidden.       $assert_session->fieldNotExists('Name', $form);
      // The source field should be shown before the vertical tabs.       $source_field_label = $media_type->getSource()->getSourceFieldDefinition($media_type)->getLabel();
      $test_source_field = $assert_session->elementExists('xpath', "//*[contains(text(), '$source_field_label')]", $form)->getOuterHtml();
      $vertical_tabs = $assert_session->elementExists('css', '.js-form-type-vertical-tabs', $form)->getOuterHtml();
      $this->assertGreaterThan(strpos($form_html$test_source_field)strpos($form_html$vertical_tabs));
      // The "Published" checkbox should be the last element.
/** * Tests the exposed form markup. */
  public function testExposedFormRender() {
    $view = Views::getView('test_exposed_form_buttons');
    $this->executeView($view);
    $exposed_form = $view->display_handler->getPlugin('exposed_form');
    $output = $exposed_form->renderExposedForm();
    $this->setRawContent(\Drupal::service('renderer')->renderRoot($output));

    $this->assertFieldByXpath('//form/@id', Html::cleanCssIdentifier('views-exposed-form-' . $view->storage->id() . '-' . $view->current_display), 'Expected form ID found.');

    $view->setDisplay('page_1');
    $expected_action = $view->display_handler->getUrlInfo()->toString();
    $this->assertFieldByXPath('//form/@action', $expected_action, 'The expected value for the action attribute was found.');
    // Make sure the description is shown.     $result = $this->xpath('//form//div[contains(@id, "edit-type--2--description") and normalize-space(text())="Exposed description"]');
    $this->assertCount(1, $result, 'Filter description was found.');
  }

  /** * Tests the exposed form raw input. */

  public static function getClass($class) {
    $class = (string) $class;
    if (!isset(static::$classes[$class])) {
      static::$classes[$class] = static::cleanCssIdentifier(mb_strtolower($class));
    }
    return static::$classes[$class];
  }

  /** * Prepares a string for use as a CSS identifier (element, class, or ID name). * * Link below shows the syntax for valid CSS identifiers (including element * names, classes, and IDs in selectors). * * @see http://www.w3.org/TR/CSS21/syndata.html#characters * * @param string $identifier * The identifier to clean. * @param array $filter * An array of string replacements to use on the identifier. * * @return string * The cleaned identifier. */
/** * Returns the raw HTML for the given field. * * @param $field_name * The name of the field for which to return the HTML. * * @return string * The raw HTML. */
  protected function getFieldHtml($field_name) {
    $css_id = Html::cleanCssIdentifier('edit-field-' . $field_name . '-wrapper');
    return $this->xpath('//*[@id=:id]', [':id' => $css_id])[0]->getHtml();
  }

}
return $elements;
  }

  /** * {@inheritdoc} */
  public function elementClasses($row_index = NULL) {
    $classes = $this->tokenizeValue($this->options['element_class']$row_index);
    $classes = explode(' ', $classes);
    foreach ($classes as &$class) {
      $class = Html::cleanCssIdentifier($class);
    }
    return implode(' ', $classes);
  }

  /** * {@inheritdoc} */
  public function tokenizeValue($value$row_index = NULL) {
    if (str_contains($value, '{{')) {
      $fake_item = [
        'alter_text' => TRUE,
        

  public function getCustomClass($result_index$type) {
    $class = $this->options[$type . '_class_custom'];
    if ($this->usesFields() && $this->view->field) {
      $class = strip_tags($this->tokenizeValue($class$result_index));
    }

    $classes = explode(' ', $class);
    foreach ($classes as &$class) {
      $class = Html::cleanCssIdentifier($class);
    }
    return implode(' ', $classes);
  }

}
$display_id = 'default';
      $option_build['#defaulted'] = TRUE;
    }
    else {
      $display_id = $display['id'];
      if (!$view->getExecutable()->displayHandlers->get($display['id'])->isDefaultDisplay()) {
        if ($view->getExecutable()->displayHandlers->get($display['id'])->defaultableSections($id)) {
          $option_build['#overridden'] = TRUE;
        }
      }
    }
    $option_build['#attributes']['class'][] = Html::cleanCssIdentifier($display_id . '-' . $id);
    return $option_build;
  }

  /** * Add information about a section to a display. */
  public function getFormBucket(ViewUI $view$type$display) {
    $executable = $view->getExecutable();
    $executable->setDisplay($display['id']);
    $executable->initStyle();

    

  public function getRowClass($row_index) {
    if ($this->usesRowClass()) {
      $class = $this->options['row_class'];
      if ($this->usesFields() && $this->view->field) {
        $class = strip_tags($this->tokenizeValue($class$row_index));
      }

      $classes = explode(' ', $class);
      foreach ($classes as &$class) {
        $class = Html::cleanCssIdentifier($class);
      }
      return implode(' ', $classes);
    }
  }

  /** * Take a value and apply token replacement logic to it. */
  public function tokenizeValue($value$row_index) {
    if (str_contains($value, '{{')) {
      // Row tokens might be empty, for example for node row style.
/** * Returns a views exposed form ID. * * @param \Drupal\views\ViewExecutable $view * The view to create an ID for. * * @return string * The form ID. */
  protected function getExpectedExposedFormId(ViewExecutable $view) {
    return Html::cleanCssIdentifier('views-exposed-form-' . $view->storage->id() . '-' . $view->current_display);
  }

  /** * Tests a view which is rendered after a form with a validation error. */
  public function testFormErrorWithExposedForm() {
    $this->drupalGet('views_test_data_error_form_page');
    $this->assertSession()->statusCodeEquals(200);
    $form = $this->cssSelect('form.views-exposed-form');
    $this->assertNotEmpty($form, 'The exposed form element was found.');
    // Ensure the exposed form is rendered before submitting the normal form.
$type = $this->drupalCreateContentType([
      'type' => 'foo',
    ]);
    $this->drupalGet("/admin/structure/types/manage/{$type->id()}/fields/add-field");

    $field_types = [
      'file',
      'image',
      'field_ui:entity_reference:media',
    ];
    $description_ids = array_map(function D$item) {
      return '#edit-description-' . Html::cleanCssIdentifier($item);
    }$field_types);

    // Choose a boolean field, none of the description containers should be     // visible.     $assert_session->optionExists('edit-new-storage-type', 'boolean');
    $page->selectFieldOption('edit-new-storage-type', 'boolean');
    foreach ($description_ids as $description_id) {
      $this->assertFalse($assert_session->elementExists('css', $description_id)->isVisible());
    }
    // Select each of the file, image, and media fields and verify their     // descriptions are now visible and match the expected text.


      $response->addCommand(new OpenModalDialogCommand($title$display$options));

      // Views provides its own custom handling of AJAX form submissions.       // Usually this happens at the same path, but custom paths may be       // specified in $form_state.       $form_url = $form_state->has('url') ? $form_state->get('url')->toString() : Url::fromRoute('<current>')->toString();
      $response->addCommand(new SetFormCommand($form_url));

      if ($section = $form_state->get('#section')) {
        $response->addCommand(new HighlightCommand('.' . Html::cleanCssIdentifier($section)));
      }

      return $response;
    }

    return $title ? ['#title' => $title, '#markup' => $output] : $output;
  }

  /** * {@inheritdoc} */
  

  public function testCleanCssIdentifier($expected$source$filter = NULL) {
    if ($filter !== NULL) {
      $this->assertSame($expected, Html::cleanCssIdentifier($source$filter));
    }
    else {
      $this->assertSame($expected, Html::cleanCssIdentifier($source));
    }
  }

  /** * Provides test data for testCleanCssIdentifier(). * * @return array * Test data. */
Home | Imprint | This part of the site doesn't use cookies.