2

I want to execute a View and export the JSON file to the files directory. I am using Views Data Export and VDE drush add-on modules. My view has a file/PDF field and I want to provide a direct link to the PDF file. So I added a Views relation for the field and a file URI formatter field. When I go to see the output the View creates at the Path. The URL is an absolute link directly to the PDF file. The URL includes the domain name when viewing it on development or production instances. And this is what I want. This is the correct URL path in the JSON:

href=\u0022http:\/\/mysite.com\/files\/Allergens.pdf

But when I use this Drush command below provided by views_data_export or vde_drush

drush vde myview myviewid myfile.json 

the domain name is stripped out like this:

href=\u0022http:\/\/default\/files\/Allergens.pdf

Why is this happening only when using the Drush command? I found I can fix it by adding this at the end of the command

--uri=mysite.com

So my new drush command looks like this:

drush vde myview myviewid myfile.json --uri=mysite.com

But doing this will hardcode the domain name into the URL. I want the URL to work on development, production and on my local. Thank you for any help you can provide.

  • 2
    I can't find any reference for now that explains it any better: On some hosting environments Drush (being executed from the command line and not from the browser) simply can't retrieve your site's $base_url. Drush also might not know it when you have a multi-site setup. In both cases you can add the absolute URL as --uri option to pass the $base_url to PHP to let the code know under which publicly accessible URL your site is located. When rebuilding sitemaps for example during drush cron. Simply adjust the --uri option based on your environment. – leymannx Feb 8 at 14:11
  • @leymannx, Thank you. I tried adding one at a time a base_url and a trusted_host setting in settings.php then ran the drush command adding this at the end --uri But the url still does not contain the domain name. – paulcap1 Feb 8 at 14:28
  • Just --uri isn't enough. You need to set an URL --uri=https://example.com. With a little bit of Bash magic you could also try to get it from an environment variable. Or from some Git-ignored file like --uri="$(< sites/SITE_NAME/uri.txt)". And most likely there's even a way to define it in some drushrc file. But I don't remember if drushrc isn't deprecated by now and don't know what the alternative is now. – leymannx Feb 8 at 14:44
  • 1
    @leymannx Thanks again. We are using a load balancer with 4 server instances. I am not sure how to create a base_url or trusted_host setting for this scenario. Add this --uri=mysite.com to the command works. I am going to try makeing this a variable using this in our custom module $hostname = \Drupal::request()->getHost(); Then the drush command would be something like this exec("/usr/local/bin/php ../vendor/drush/drush/drush.php vde myview myviewid ".$write_dir."/search-for-guidance.json" --uri=".$hostname."); – paulcap1 Feb 8 at 16:53
0

You can set the default uri by adding the following file. Then if you don't provide --uri it'll use the default.

sites/all/drush/drush.yml

options:
  uri: "https://your-site.domain"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.