Drupal 8 change url to absolute - drupal-modules

I want to change my all internal url to absolute from relative as of now.
For e.g. my menu link urls are "/about" as so on I want this to be absolute url like xyz.com/about.
How can I do this ?

From the code that would be :
use Drupal\Core\Url;
$option = ['absolute' => TRUE];
$url = Url::fromUserInput('/about', $option);

Related

extract string from req.url.path in VCL Fastly

I need help to extract values from string req.url.path which looks like this:
/a/b/c/d
Need to extract c, if there is url, like /a, it should return ''.
I tried
regsub(req.url.path, "/a/b/", "\5");
Tried with replace too.
but it is not effectively working for me. Please help me.
I believe the following might be along the lines of what you require:
if (req.url.path ~ "^/a/.+/c") {
set req.url = regsub(req.url.path, "/c", "") + "?" + req.url.qs;
}
It checks if the URL path begins with /a and is followed by /c (presuming also that there are segments in-between, such as /b as per your given example /a/b/c/d).
Here is a Fastly Fiddle link for you to play around with the code:
https://fiddle.fastlydemo.net/fiddle/527480ba
So given the input URL path:
/a/b/c/d?id=testing
It would change to:
/a/b/d?id=testing
Notice that the /c has been extracted from the path.

How to use variable in url?

I'm trying to develop a maco in python. I need a method that changes the URI given in the input variables.
For instance, in www.(variable).com I need to change the URL www.1.com to www.2.com
You can use formatted strings like this:
variable = 1
url = f"www.{variable}.com"
You could store your list of names in a list and then interpolate the variable into the f-string like:
names = ['google', 'amazon', 'microsoft']
for name in names:
print(f"www.{name}.com")
OUTPUT
www.google.com
www.amazon.com
www.microsoft.com

Custom Browser Search Engines and Multiple Parameters

I have been working with custom search engines in Chrome and Vivaldi, which have been absolutely fantastic. However, I have only been able to successfully perform searches that replace a single parameter (e.g., the %s in the following URL: http://www.thesaurus.com/browse/%s) with my search term (e.g., http://www.thesaurus.com/browse/trifocal).
Is it possible to replace multiple parameters for a custom browser search engine so I could not only specify a search term but also the webpage to search? For example, I would type the custom search engine shortcut "d" followed by "thesaurus, trifocal", which would input the two parameters into a predefined URL and search the word "trifocal" on "thesaurus.com".
Basically, I want to be able to search for multiple values at two different points in the custom search engine for scenarios where multiple sites use the same "base" url where the only difference is a word or two.
Please let me know if you have any questions.
You can use JavaScript. Take the entire string as input to the function. Split it based on some character and them trim the parts. Construct the link and then open it within the current tab or in a new tab, depending on which suits you.
javascript:(function myFunction(str) {
var part = ";";
var res = str.split(part);
var search_query = res[0].trim();
var category = res[1].trim();
var new_tab = res[2];
var res_url = "https://github.com/search?q=" + search_query + "&type=" + category;
// open in new tab if str contains a third parameter that is composed of zero or more spaces
if ((new_tab !== undefined) && (new_tab.trim() === ""))
{
window.open(res_url, "_blank");
}
else // open in current tab
{
window.location.href = res_url;
}
})('%s');
On minifying, the custom search engine URL would be javascript:(function myFunction(str){var part=";";var res=str.split(part);var search_query=res[0].trim();var category=res[1].trim();var new_tab=res[2];var res_url="https://github.com/search?q="+search_query+"&type="+category;if((new_tab!==undefined)&&(new_tab.trim()==="")){window.open(res_url,"_blank")}else{window.location.href=res_url}})('%s');. The function is called with '%s' as the parameter. It splits based on the semicolon character.
Usage: Let's nickname this custom search engine as, say, "gm". Then a search for "gm binary tree; code" will open https://github.com/search?q=binary%20tree&type=code in the current page. A search for "gm radix tree; commits; " will open https://github.com/search?q=radix%20tree&type=commits in a new tab.
Note that this function would fail if %s contains single quote(s). It also doesn't work if called from an internal page like Settings.
An alternative to accomplish your goal would be to ditch the custom search engine idea and use a scripting tool like AutoHotkey (Windows) to replace search strings by URLs.

how to get the data in url using groovy code?

i want to get defect id from the url using groovy code (To build custom code in tasktop).
for example: I will have an dynamic url generated say www.xyz.com/abc/defect_123/ now I want to retrieve that letter that always starts from 17th position. and return the string
Please help..
Thanks in advance
Here are two possibilities. Please note that the "substring" option is very strict and will always start from the 16th position (what happens if the domain changes from www.xyz.com to www.xyzw.com?)
def str = 'www.xyz.com/abc/defect_123/';
def pieces = str.tokenize('/'); // prints defect_123
def from16 = str.substring(16); // prints defect_123/
println from16;
println pieces.last();
You should define this as dynamic url in UrlMappings.groovy file:
"www.xyz.com/abc/$defect_id" (controller: 'YourController', action: 'method_name')
and you can access the defect_id variable from YourController using params.defect_id

Regex for href value in Groovy

What's the best way in Groovy to capture a link's href value with regex?
example: some link
I want to capture just what's in bold.
It depends on what you're picking the anchor tag out of, but if you have that isolated to what you have shown, the following should work:
def link= """some "link"""
def url = (x =~ /\"(.*?)\"/)[0][1]

Resources