Loading opensearchtab.module +8 −5 Changes for opensearchtab.module: 8 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -15,13 +15,14 @@ use Drupal\Core\Routing\RouteMatchInterface; */ function opensearchtab_page_attachments(array &$page) { if (\Drupal::service('path.matcher')->isFrontPage()) { if (Drupal::service('path.matcher')->isFrontPage()) { $link = [ '#tag' => 'link', '#attributes' => [ 'type' => 'application/opensearchdescription+xml', 'rel' => 'search', 'href' => \Drupal::request()->getSchemeAndHttpHost() . '/opensearchdescription', 'href' => Drupal::request() ->getSchemeAndHttpHost() . '/opensearchdescription', ], ]; $page['#attached']['html_head'][] = [$link, 'link']; Loading @@ -35,13 +36,15 @@ function opensearchtab_help($route_name, RouteMatchInterface $route_match) { switch ($route_name) { case 'help.page.opensearchtab': $text = file_get_contents(__DIR__ . '/README.md'); if (!\Drupal::moduleHandler()->moduleExists('markdown')) { if (!Drupal::moduleHandler()->moduleExists('markdown')) { return '<pre>' . Html::escape($text) . '</pre>'; } else { // Use the Markdown filter to render the README. $filter_manager = \Drupal::service('plugin.manager.filter'); $settings = \Drupal::configFactory()->get('markdown.settings')->getRawData(); $filter_manager = Drupal::service('plugin.manager.filter'); $settings = Drupal::configFactory() ->get('markdown.settings') ->getRawData(); $config = ['settings' => $settings]; $filter = $filter_manager->createInstance('markdown', $config); return $filter->process($text, 'en'); Loading src/Controller/OpenSearchDescriptionController.php +40 −12 Changes for src/Controller/OpenSearchDescriptionController.php: 40 added lines, 12 removed lines. Original line number Diff line number Diff line Loading @@ -16,18 +16,42 @@ class OpenSearchDescriptionController { // Prepare variables for the XML content. $config = \Drupal::config('system.site'); $ost_config = \Drupal::config('opensearchtab.settings'); $short_name = !empty($ost_config->get('shortname')) ? $ost_config->get('shortname') : (!empty($config->get('name')) ? $config->get('name') : 'Drupal site'); $description = !empty($ost_config->get('description')) ? $ost_config->get('description') : (!empty($config->get('slogan')) ? $config->get('slogan') : 'Search the site'); $search_form_path = !empty($ost_config->get('search-form-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('search-form-path') : \Drupal::request()->getSchemeAndHttpHost() . '/search/node'; $search_path = !empty($ost_config->get('search-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('search-path') : \Drupal::request()->getSchemeAndHttpHost() . '/search/node?keys={searchTerms}'; $suggestions_path_json = !empty($ost_config->get('suggestions-path-json')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('suggestions-path-json') : ''; $suggestions_path_xml = !empty($ost_config->get('suggestions-path-xml')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('suggestions-path-xml') : ''; $favicon_path = !empty($ost_config->get('favicon-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('favicon-path') : ''; $short_name = !empty($ost_config->get('shortname')) ? $ost_config->get('shortname') : (!empty($config->get('name')) ? $config->get('name') : 'Drupal site'); $description = !empty($ost_config->get('description')) ? $ost_config->get('description') : (!empty($config->get('slogan')) ? $config->get('slogan') : 'Search the site'); $search_form_path = !empty($ost_config->get('search-form-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('search-form-path') : \Drupal::request()->getSchemeAndHttpHost() . '/search/node'; $search_path = !empty($ost_config->get('search-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('search-path') : \Drupal::request()->getSchemeAndHttpHost() . '/search/node?keys={searchTerms}'; $suggestions_path_json = !empty($ost_config->get('suggestions-path-json')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('suggestions-path-json') : ''; $suggestions_path_xml = !empty($ost_config->get('suggestions-path-xml')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('suggestions-path-xml') : ''; $favicon_path = !empty($ost_config->get('favicon-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('favicon-path') : ''; // Define the XML content. $openTag = new \SimpleXMLElement('<OpenSearchDescription></OpenSearchDescription>'); $openTag->addAttribute('xmlns', 'http://a9.com/-/spec/opensearch/1.1/'); $openTag->addAttribute('xmlns:xmlns:moz', 'http://www.mozilla.org/2006/browser/search/'); $openTag = new \SimpleXMLElement( '<OpenSearchDescription></OpenSearchDescription>' ); $openTag->addAttribute( 'xmlns', 'http://a9.com/-/spec/opensearch/1.1/' ); $openTag->addAttribute( 'xmlns:xmlns:moz', 'http://www.mozilla.org/2006/browser/search/' ); $openTag->addChild('ShortName', $short_name); $openTag->addChild('Description', $description); $searchUrl = $openTag->addChild('Url'); Loading @@ -36,13 +60,17 @@ class OpenSearchDescriptionController { $searchUrl->addAttribute('template', $search_path); if ($suggestions_path_json !== '') { $suggestionsUrl = $openTag->addChild('Url'); $suggestionsUrl->addAttribute('type', 'application/x-suggestions+json'); $suggestionsUrl->addAttribute( 'type', 'application/x-suggestions+json' ); $suggestionsUrl->addAttribute('method', 'get'); $suggestionsUrl->addAttribute('template', $suggestions_path_json); } if ($suggestions_path_xml !== '') { $suggestionsUrl = $openTag->addChild('Url'); $suggestionsUrl->addAttribute('type', 'application/x-suggestions+xml'); $suggestionsUrl->addAttribute( 'type', 'application/x-suggestions+xml' ); $suggestionsUrl->addAttribute('method', 'get'); $suggestionsUrl->addAttribute('template', $suggestions_path_xml); } Loading src/Form/OpenSearchSettingsForm.php +37 −22 Changes for src/Form/OpenSearchSettingsForm.php: 37 added lines, 22 removed lines. Original line number Diff line number Diff line Loading @@ -19,13 +19,6 @@ class OpenSearchSettingsForm extends ConfigFormBase { return 'opensearchtab_settings_form'; } /** * {@inheritdoc} */ protected function getEditableConfigNames() { return ['opensearchtab.settings']; } /** * {@inheritdoc} */ Loading @@ -33,7 +26,8 @@ class OpenSearchSettingsForm extends ConfigFormBase { $config = $this->config('opensearchtab.settings'); $opensearchdescription_link = Link::fromTextAndUrl('Open Search Description', Url::fromRoute('opensearchtab.opensearchdescription'))->toString(); $opensearchdescription_link = Link::fromTextAndUrl('Open Search Description', Url::fromRoute('opensearchtab.opensearchdescription'))->toString(); $info_text = "See the XML of the open search tab: $opensearchdescription_link"; Loading Loading @@ -102,43 +96,50 @@ class OpenSearchSettingsForm extends ConfigFormBase { public function validateForm(array &$form, FormStateInterface $form_state) { if (!$form_state->isValueEmpty('opensearchtab_search_form_path')) { if (strlen($form_state->getValue('opensearchtab_search_form_path')) < 2) { $form_state->setErrorByName('opensearchtab_search_form_path', $this->t('Path is less than 2 characters')); $form_state->setErrorByName('opensearchtab_search_form_path', $this->t('Path is less than 2 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_search_path')) { if (strlen($form_state->getValue('opensearchtab_search_path')) < 15) { $form_state->setErrorByName('opensearchtab_search_path', $this->t('Path is less than 15 characters')); $form_state->setErrorByName('opensearchtab_search_path', $this->t('Path is less than 15 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_suggestions_path_json')) { if (strlen($form_state->getValue('opensearchtab_suggestions_path_json')) < 15) { $form_state->setErrorByName('opensearchtab_suggestions_path_json', $this->t('Path is less than 15 characters')); $form_state->setErrorByName('opensearchtab_suggestions_path_json', $this->t('Path is less than 15 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_suggestions_path_xml')) { if (strlen($form_state->getValue('opensearchtab_suggestions_path_xml')) < 15) { $form_state->setErrorByName('opensearchtab_suggestions_path_xml', $this->t('Path is less than 15 characters')); $form_state->setErrorByName('opensearchtab_suggestions_path_xml', $this->t('Path is less than 15 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_shortname')) { if (strlen($form_state->getValue('opensearchtab_shortname')) < 3) { $form_state->setErrorByName('opensearchtab_shortname', $this->t('Short name is less than 3 characters')); $form_state->setErrorByName('opensearchtab_shortname', $this->t('Short name is less than 3 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_description')) { if (strlen($form_state->getValue('opensearchtab_description')) < 10) { $form_state->setErrorByName('opensearchtab_description', $this->t('Description is less than 10 characters')); $form_state->setErrorByName('opensearchtab_description', $this->t('Description is less than 10 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_favicon_path')) { if (strlen($form_state->getValue('opensearchtab_favicon_path')) < 5) { $form_state->setErrorByName('opensearchtab_favicon_path', $this->t('Path is less than 5 characters')); $form_state->setErrorByName('opensearchtab_favicon_path', $this->t('Path is less than 5 characters')); } } } Loading @@ -149,14 +150,28 @@ class OpenSearchSettingsForm extends ConfigFormBase { public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); $this->config('opensearchtab.settings') ->set('search-form-path', $form_state->getValue('opensearchtab_search_form_path')) ->set('search-path', $form_state->getValue('opensearchtab_search_path')) ->set('suggestions-path-json', $form_state->getValue('opensearchtab_suggestions_path_json')) ->set('suggestions-path-xml', $form_state->getValue('opensearchtab_suggestions_path_xml')) ->set('shortname', $form_state->getValue('opensearchtab_shortname')) ->set('description', $form_state->getValue('opensearchtab_description')) ->set('favicon-path', $form_state->getValue('opensearchtab_favicon_path')) ->set('search-form-path', $form_state->getValue('opensearchtab_search_form_path')) ->set('search-path', $form_state->getValue('opensearchtab_search_path')) ->set('suggestions-path-json', $form_state->getValue('opensearchtab_suggestions_path_json')) ->set('suggestions-path-xml', $form_state->getValue('opensearchtab_suggestions_path_xml')) ->set('shortname', $form_state->getValue('opensearchtab_shortname')) ->set('description', $form_state->getValue('opensearchtab_description')) ->set('favicon-path', $form_state->getValue('opensearchtab_favicon_path')) ->save(); } /** * {@inheritdoc} */ protected function getEditableConfigNames() { return ['opensearchtab.settings']; } } src/Tests/OpenSearchDescriptionTest.php +19 −13 Changes for src/Tests/OpenSearchDescriptionTest.php: 19 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -19,13 +19,6 @@ class OpenSearchDescriptionTest extends BrowserTestBase { */ public static $modules = ['opensearchtab']; /** * A simple user with 'access content' permission. * * @var \Drupal\user\Entity\User */ private $user; /** * Default theme. * Loading @@ -35,13 +28,21 @@ class OpenSearchDescriptionTest extends BrowserTestBase { */ protected $defaultTheme = 'stark'; /** * A simple user with 'access content' permission. * * @var \Drupal\user\Entity\User */ private $user; /** * Perform any initial set up tasks that run before every test method. */ public function setUp() { parent::setUp(); $this->user = $this->drupalCreateUser(['access content']); $this->config = \Drupal::configFactory()->getEditable('opensearchtab.settings'); $this->config = Drupal::configFactory() ->getEditable('opensearchtab.settings'); $this->config ->set('search-form-path', '/my-search') ->set('search-path', '/my-search?{searchTerms}') Loading Loading @@ -70,11 +71,16 @@ class OpenSearchDescriptionTest extends BrowserTestBase { $this->assertRaw('</OpenSearchDescription>'); $this->assertRaw('<ShortName>My site</ShortName>'); $this->assertRaw('<Description>My site search</Description>'); $this->assertRaw('<Url type="text/html" method="get" template="' . \Drupal::request()->getSchemeAndHttpHost() . '/my-search?{searchTerms}"/>'); $this->assertRaw('<Url type="application/x-suggestions+json" method="get" template="' . \Drupal::request()->getSchemeAndHttpHost() . '/suggestions?{searchTerms}"/>'); $this->assertRaw('<Url type="application/x-suggestions+xml" method="get" template="' . \Drupal::request()->getSchemeAndHttpHost() . '/suggestions?{searchTerms}"/>'); $this->assertRaw('<moz:SearchForm>' . \Drupal::request()->getSchemeAndHttpHost() . '/my-search</moz:SearchForm>'); $this->assertRaw('<Image height="16" width="16" type="image/x-icon">' . \Drupal::request()->getSchemeAndHttpHost() . '/themes/contrib/mytheme/favicon.ico</Image>'); $this->assertRaw('<Url type="text/html" method="get" template="' . Drupal::request() ->getSchemeAndHttpHost() . '/my-search?{searchTerms}"/>'); $this->assertRaw('<Url type="application/x-suggestions+json" method="get" template="' . Drupal::request() ->getSchemeAndHttpHost() . '/suggestions?{searchTerms}"/>'); $this->assertRaw('<Url type="application/x-suggestions+xml" method="get" template="' . Drupal::request() ->getSchemeAndHttpHost() . '/suggestions?{searchTerms}"/>'); $this->assertRaw('<moz:SearchForm>' . Drupal::request() ->getSchemeAndHttpHost() . '/my-search</moz:SearchForm>'); $this->assertRaw('<Image height="16" width="16" type="image/x-icon">' . Drupal::request() ->getSchemeAndHttpHost() . '/themes/contrib/mytheme/favicon.ico</Image>'); } } src/Tests/OpenSearchSettingsFormTest.php +10 −10 Changes for src/Tests/OpenSearchSettingsFormTest.php: 10 added lines, 10 removed lines. Original line number Diff line number Diff line Loading @@ -20,6 +20,15 @@ class OpenSearchSettingsFormTest extends BrowserTestBase { */ public static $modules = ['opensearchtab']; /** * Default theme. * * See: https://www.drupal.org/node/3083055 * * @var string */ protected $defaultTheme = 'stark'; /** * The role anonymous user. * Loading Loading @@ -55,15 +64,6 @@ class OpenSearchSettingsFormTest extends BrowserTestBase { */ private $guestUser; /** * Default theme. * * See: https://www.drupal.org/node/3083055 * * @var string */ protected $defaultTheme = 'stark'; /** * Perform any initial set up tasks that run before every test method. * Loading @@ -77,7 +77,7 @@ class OpenSearchSettingsFormTest extends BrowserTestBase { $this->guestPermissions = $this->guestRole->getPermissions(); $this->guestUser = $this->drupalCreateUser($this->guestPermissions); $this->adminPermissions = array_keys(\Drupal::service( $this->adminPermissions = array_keys(Drupal::service( 'user.permissions')->getPermissions() ); $this->adminUser = $this->drupalCreateUser($this->adminPermissions); Loading Loading
opensearchtab.module +8 −5 Changes for opensearchtab.module: 8 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -15,13 +15,14 @@ use Drupal\Core\Routing\RouteMatchInterface; */ function opensearchtab_page_attachments(array &$page) { if (\Drupal::service('path.matcher')->isFrontPage()) { if (Drupal::service('path.matcher')->isFrontPage()) { $link = [ '#tag' => 'link', '#attributes' => [ 'type' => 'application/opensearchdescription+xml', 'rel' => 'search', 'href' => \Drupal::request()->getSchemeAndHttpHost() . '/opensearchdescription', 'href' => Drupal::request() ->getSchemeAndHttpHost() . '/opensearchdescription', ], ]; $page['#attached']['html_head'][] = [$link, 'link']; Loading @@ -35,13 +36,15 @@ function opensearchtab_help($route_name, RouteMatchInterface $route_match) { switch ($route_name) { case 'help.page.opensearchtab': $text = file_get_contents(__DIR__ . '/README.md'); if (!\Drupal::moduleHandler()->moduleExists('markdown')) { if (!Drupal::moduleHandler()->moduleExists('markdown')) { return '<pre>' . Html::escape($text) . '</pre>'; } else { // Use the Markdown filter to render the README. $filter_manager = \Drupal::service('plugin.manager.filter'); $settings = \Drupal::configFactory()->get('markdown.settings')->getRawData(); $filter_manager = Drupal::service('plugin.manager.filter'); $settings = Drupal::configFactory() ->get('markdown.settings') ->getRawData(); $config = ['settings' => $settings]; $filter = $filter_manager->createInstance('markdown', $config); return $filter->process($text, 'en'); Loading
src/Controller/OpenSearchDescriptionController.php +40 −12 Changes for src/Controller/OpenSearchDescriptionController.php: 40 added lines, 12 removed lines. Original line number Diff line number Diff line Loading @@ -16,18 +16,42 @@ class OpenSearchDescriptionController { // Prepare variables for the XML content. $config = \Drupal::config('system.site'); $ost_config = \Drupal::config('opensearchtab.settings'); $short_name = !empty($ost_config->get('shortname')) ? $ost_config->get('shortname') : (!empty($config->get('name')) ? $config->get('name') : 'Drupal site'); $description = !empty($ost_config->get('description')) ? $ost_config->get('description') : (!empty($config->get('slogan')) ? $config->get('slogan') : 'Search the site'); $search_form_path = !empty($ost_config->get('search-form-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('search-form-path') : \Drupal::request()->getSchemeAndHttpHost() . '/search/node'; $search_path = !empty($ost_config->get('search-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('search-path') : \Drupal::request()->getSchemeAndHttpHost() . '/search/node?keys={searchTerms}'; $suggestions_path_json = !empty($ost_config->get('suggestions-path-json')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('suggestions-path-json') : ''; $suggestions_path_xml = !empty($ost_config->get('suggestions-path-xml')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('suggestions-path-xml') : ''; $favicon_path = !empty($ost_config->get('favicon-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('favicon-path') : ''; $short_name = !empty($ost_config->get('shortname')) ? $ost_config->get('shortname') : (!empty($config->get('name')) ? $config->get('name') : 'Drupal site'); $description = !empty($ost_config->get('description')) ? $ost_config->get('description') : (!empty($config->get('slogan')) ? $config->get('slogan') : 'Search the site'); $search_form_path = !empty($ost_config->get('search-form-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('search-form-path') : \Drupal::request()->getSchemeAndHttpHost() . '/search/node'; $search_path = !empty($ost_config->get('search-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('search-path') : \Drupal::request()->getSchemeAndHttpHost() . '/search/node?keys={searchTerms}'; $suggestions_path_json = !empty($ost_config->get('suggestions-path-json')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('suggestions-path-json') : ''; $suggestions_path_xml = !empty($ost_config->get('suggestions-path-xml')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('suggestions-path-xml') : ''; $favicon_path = !empty($ost_config->get('favicon-path')) ? \Drupal::request()->getSchemeAndHttpHost() . $ost_config->get('favicon-path') : ''; // Define the XML content. $openTag = new \SimpleXMLElement('<OpenSearchDescription></OpenSearchDescription>'); $openTag->addAttribute('xmlns', 'http://a9.com/-/spec/opensearch/1.1/'); $openTag->addAttribute('xmlns:xmlns:moz', 'http://www.mozilla.org/2006/browser/search/'); $openTag = new \SimpleXMLElement( '<OpenSearchDescription></OpenSearchDescription>' ); $openTag->addAttribute( 'xmlns', 'http://a9.com/-/spec/opensearch/1.1/' ); $openTag->addAttribute( 'xmlns:xmlns:moz', 'http://www.mozilla.org/2006/browser/search/' ); $openTag->addChild('ShortName', $short_name); $openTag->addChild('Description', $description); $searchUrl = $openTag->addChild('Url'); Loading @@ -36,13 +60,17 @@ class OpenSearchDescriptionController { $searchUrl->addAttribute('template', $search_path); if ($suggestions_path_json !== '') { $suggestionsUrl = $openTag->addChild('Url'); $suggestionsUrl->addAttribute('type', 'application/x-suggestions+json'); $suggestionsUrl->addAttribute( 'type', 'application/x-suggestions+json' ); $suggestionsUrl->addAttribute('method', 'get'); $suggestionsUrl->addAttribute('template', $suggestions_path_json); } if ($suggestions_path_xml !== '') { $suggestionsUrl = $openTag->addChild('Url'); $suggestionsUrl->addAttribute('type', 'application/x-suggestions+xml'); $suggestionsUrl->addAttribute( 'type', 'application/x-suggestions+xml' ); $suggestionsUrl->addAttribute('method', 'get'); $suggestionsUrl->addAttribute('template', $suggestions_path_xml); } Loading
src/Form/OpenSearchSettingsForm.php +37 −22 Changes for src/Form/OpenSearchSettingsForm.php: 37 added lines, 22 removed lines. Original line number Diff line number Diff line Loading @@ -19,13 +19,6 @@ class OpenSearchSettingsForm extends ConfigFormBase { return 'opensearchtab_settings_form'; } /** * {@inheritdoc} */ protected function getEditableConfigNames() { return ['opensearchtab.settings']; } /** * {@inheritdoc} */ Loading @@ -33,7 +26,8 @@ class OpenSearchSettingsForm extends ConfigFormBase { $config = $this->config('opensearchtab.settings'); $opensearchdescription_link = Link::fromTextAndUrl('Open Search Description', Url::fromRoute('opensearchtab.opensearchdescription'))->toString(); $opensearchdescription_link = Link::fromTextAndUrl('Open Search Description', Url::fromRoute('opensearchtab.opensearchdescription'))->toString(); $info_text = "See the XML of the open search tab: $opensearchdescription_link"; Loading Loading @@ -102,43 +96,50 @@ class OpenSearchSettingsForm extends ConfigFormBase { public function validateForm(array &$form, FormStateInterface $form_state) { if (!$form_state->isValueEmpty('opensearchtab_search_form_path')) { if (strlen($form_state->getValue('opensearchtab_search_form_path')) < 2) { $form_state->setErrorByName('opensearchtab_search_form_path', $this->t('Path is less than 2 characters')); $form_state->setErrorByName('opensearchtab_search_form_path', $this->t('Path is less than 2 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_search_path')) { if (strlen($form_state->getValue('opensearchtab_search_path')) < 15) { $form_state->setErrorByName('opensearchtab_search_path', $this->t('Path is less than 15 characters')); $form_state->setErrorByName('opensearchtab_search_path', $this->t('Path is less than 15 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_suggestions_path_json')) { if (strlen($form_state->getValue('opensearchtab_suggestions_path_json')) < 15) { $form_state->setErrorByName('opensearchtab_suggestions_path_json', $this->t('Path is less than 15 characters')); $form_state->setErrorByName('opensearchtab_suggestions_path_json', $this->t('Path is less than 15 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_suggestions_path_xml')) { if (strlen($form_state->getValue('opensearchtab_suggestions_path_xml')) < 15) { $form_state->setErrorByName('opensearchtab_suggestions_path_xml', $this->t('Path is less than 15 characters')); $form_state->setErrorByName('opensearchtab_suggestions_path_xml', $this->t('Path is less than 15 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_shortname')) { if (strlen($form_state->getValue('opensearchtab_shortname')) < 3) { $form_state->setErrorByName('opensearchtab_shortname', $this->t('Short name is less than 3 characters')); $form_state->setErrorByName('opensearchtab_shortname', $this->t('Short name is less than 3 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_description')) { if (strlen($form_state->getValue('opensearchtab_description')) < 10) { $form_state->setErrorByName('opensearchtab_description', $this->t('Description is less than 10 characters')); $form_state->setErrorByName('opensearchtab_description', $this->t('Description is less than 10 characters')); } } if (!$form_state->isValueEmpty('opensearchtab_favicon_path')) { if (strlen($form_state->getValue('opensearchtab_favicon_path')) < 5) { $form_state->setErrorByName('opensearchtab_favicon_path', $this->t('Path is less than 5 characters')); $form_state->setErrorByName('opensearchtab_favicon_path', $this->t('Path is less than 5 characters')); } } } Loading @@ -149,14 +150,28 @@ class OpenSearchSettingsForm extends ConfigFormBase { public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); $this->config('opensearchtab.settings') ->set('search-form-path', $form_state->getValue('opensearchtab_search_form_path')) ->set('search-path', $form_state->getValue('opensearchtab_search_path')) ->set('suggestions-path-json', $form_state->getValue('opensearchtab_suggestions_path_json')) ->set('suggestions-path-xml', $form_state->getValue('opensearchtab_suggestions_path_xml')) ->set('shortname', $form_state->getValue('opensearchtab_shortname')) ->set('description', $form_state->getValue('opensearchtab_description')) ->set('favicon-path', $form_state->getValue('opensearchtab_favicon_path')) ->set('search-form-path', $form_state->getValue('opensearchtab_search_form_path')) ->set('search-path', $form_state->getValue('opensearchtab_search_path')) ->set('suggestions-path-json', $form_state->getValue('opensearchtab_suggestions_path_json')) ->set('suggestions-path-xml', $form_state->getValue('opensearchtab_suggestions_path_xml')) ->set('shortname', $form_state->getValue('opensearchtab_shortname')) ->set('description', $form_state->getValue('opensearchtab_description')) ->set('favicon-path', $form_state->getValue('opensearchtab_favicon_path')) ->save(); } /** * {@inheritdoc} */ protected function getEditableConfigNames() { return ['opensearchtab.settings']; } }
src/Tests/OpenSearchDescriptionTest.php +19 −13 Changes for src/Tests/OpenSearchDescriptionTest.php: 19 added lines, 13 removed lines. Original line number Diff line number Diff line Loading @@ -19,13 +19,6 @@ class OpenSearchDescriptionTest extends BrowserTestBase { */ public static $modules = ['opensearchtab']; /** * A simple user with 'access content' permission. * * @var \Drupal\user\Entity\User */ private $user; /** * Default theme. * Loading @@ -35,13 +28,21 @@ class OpenSearchDescriptionTest extends BrowserTestBase { */ protected $defaultTheme = 'stark'; /** * A simple user with 'access content' permission. * * @var \Drupal\user\Entity\User */ private $user; /** * Perform any initial set up tasks that run before every test method. */ public function setUp() { parent::setUp(); $this->user = $this->drupalCreateUser(['access content']); $this->config = \Drupal::configFactory()->getEditable('opensearchtab.settings'); $this->config = Drupal::configFactory() ->getEditable('opensearchtab.settings'); $this->config ->set('search-form-path', '/my-search') ->set('search-path', '/my-search?{searchTerms}') Loading Loading @@ -70,11 +71,16 @@ class OpenSearchDescriptionTest extends BrowserTestBase { $this->assertRaw('</OpenSearchDescription>'); $this->assertRaw('<ShortName>My site</ShortName>'); $this->assertRaw('<Description>My site search</Description>'); $this->assertRaw('<Url type="text/html" method="get" template="' . \Drupal::request()->getSchemeAndHttpHost() . '/my-search?{searchTerms}"/>'); $this->assertRaw('<Url type="application/x-suggestions+json" method="get" template="' . \Drupal::request()->getSchemeAndHttpHost() . '/suggestions?{searchTerms}"/>'); $this->assertRaw('<Url type="application/x-suggestions+xml" method="get" template="' . \Drupal::request()->getSchemeAndHttpHost() . '/suggestions?{searchTerms}"/>'); $this->assertRaw('<moz:SearchForm>' . \Drupal::request()->getSchemeAndHttpHost() . '/my-search</moz:SearchForm>'); $this->assertRaw('<Image height="16" width="16" type="image/x-icon">' . \Drupal::request()->getSchemeAndHttpHost() . '/themes/contrib/mytheme/favicon.ico</Image>'); $this->assertRaw('<Url type="text/html" method="get" template="' . Drupal::request() ->getSchemeAndHttpHost() . '/my-search?{searchTerms}"/>'); $this->assertRaw('<Url type="application/x-suggestions+json" method="get" template="' . Drupal::request() ->getSchemeAndHttpHost() . '/suggestions?{searchTerms}"/>'); $this->assertRaw('<Url type="application/x-suggestions+xml" method="get" template="' . Drupal::request() ->getSchemeAndHttpHost() . '/suggestions?{searchTerms}"/>'); $this->assertRaw('<moz:SearchForm>' . Drupal::request() ->getSchemeAndHttpHost() . '/my-search</moz:SearchForm>'); $this->assertRaw('<Image height="16" width="16" type="image/x-icon">' . Drupal::request() ->getSchemeAndHttpHost() . '/themes/contrib/mytheme/favicon.ico</Image>'); } }
src/Tests/OpenSearchSettingsFormTest.php +10 −10 Changes for src/Tests/OpenSearchSettingsFormTest.php: 10 added lines, 10 removed lines. Original line number Diff line number Diff line Loading @@ -20,6 +20,15 @@ class OpenSearchSettingsFormTest extends BrowserTestBase { */ public static $modules = ['opensearchtab']; /** * Default theme. * * See: https://www.drupal.org/node/3083055 * * @var string */ protected $defaultTheme = 'stark'; /** * The role anonymous user. * Loading Loading @@ -55,15 +64,6 @@ class OpenSearchSettingsFormTest extends BrowserTestBase { */ private $guestUser; /** * Default theme. * * See: https://www.drupal.org/node/3083055 * * @var string */ protected $defaultTheme = 'stark'; /** * Perform any initial set up tasks that run before every test method. * Loading @@ -77,7 +77,7 @@ class OpenSearchSettingsFormTest extends BrowserTestBase { $this->guestPermissions = $this->guestRole->getPermissions(); $this->guestUser = $this->drupalCreateUser($this->guestPermissions); $this->adminPermissions = array_keys(\Drupal::service( $this->adminPermissions = array_keys(Drupal::service( 'user.permissions')->getPermissions() ); $this->adminUser = $this->drupalCreateUser($this->adminPermissions); Loading