Jump to content
alkahest

Image paths when bootstrapping PW?

Recommended Posts

I'm having a weird situation where I'm trying to get the URL for an image attached to a page, but I'm getting back the wrong paths.

The URL is mysite.com/5gum/. My file structure is like this:

/admin/ <-- ProcessWire is in here
-- /site/
-- /wire/

/5gum/ <-- my site templates are in here 
-- index.php (calls header.php below)

/assets/ 
-- /includes/ 
---- header.php

I'm bootstrapping PW into header.php of a file under /5gum/.

I'm trying to do something like this:

$brand_slug = basename ($_SERVER['REQUEST_URI']);  // this would get me "5gum"
$brand = wire('pages')->get("name=".$brand_slug); // this gives me a specific page
echo $brand->meta_image->url; // this should output the path to an image attached to the page

If meta_image is a field that accepts a maximum of 1 image, shouldn't this return the full URL to the image? Instead, it returns:

/5gum/site/assets/files/1027/

What doesn't make sense to me is that the root of the site isn't /5gum/, it should be /admin/ right, according to PW? Do I need to set the root somehow?

Share this post


Link to post
Share on other sites

I don't really get your setup. Pretty exotic.

What about /site/templates/ ?

I'm not sure, but if you don't have your PW installed in a subfolder /5gum/ then it shouldn't include in it urls. But as I said I don't really get your setup here.

Could be that you have the /5gum/index.php rendering something? Why do you have it this way?

To your problem of the image url. It's simply because you do this in a bootstrap and there's no output formatting happening there. So if there's no output formatting any image field will be an array, regardless of the image field settings (set to max 1).

So this will get your image

echo $brand->meta_image->first->url;
  • Like 1

Share this post


Link to post
Share on other sites

Ahhh, that does the trick! When I do $brand->meta_image->first->url, I get the filename as well.

However it still outputs /5gum/ as the root:

/5gum/site/assets/files/1033/slim_cobalt.png

I know my setup is strange, basically there is a brand under each directory in the top-level domain:

/5gum/

/some-other-brand/

/another-brand/

And they all share the same assets (images, markup, etc) from the /assets/ folder. By visiting /5gum/, the system pulls in all the includes it needs from /assets/includes/ like the footer.php and header.php and other files. However all PW is managing in this case is order information, so that's why it's bootstrapped in and cordoned off in its own folder under /admin/. I'm not using any template file output from PW, I just talk to PW via Ajax to send/retrieve order information (and some basic information about the products).

What I suspect is that somehow PW thinks /5gum/ is the root of the site. In fact, when I ask for:

$config->paths->root

PW says the root is /5gum/ as opposed to /admin/. Is there a way to tell PW what the root of the site is?



(And yes, if you go to mysite.com/5gum/ you hit index.php, which calls in header.php, which is where PW is bootstrapped.)

Share this post


Link to post
Share on other sites

PW is looking at the request URL. Since your request URL is /5gum/, PW thinks that's the starting point. You may need to set $config->urls->root manually to /admin/.

This bit of code is probably not so safe: 

$brand_slug = basename ($_SERVER['REQUEST_URI']);  // this would get me "5gum"
$brand = wire('pages')->get("name=".$brand_slug); // this gives me a specific page
I'd suggest changing it to:
// sanitize brand slug with the pageName function
$brand_slug = wire('sanitizer')->pageName(basename($_SERVER['REQUEST_URI']));  // this would get me "5gum"
$brand = wire('pages')->get("name=$brand_slug"); // this gives me a specific page

// double check that brand is what you expect and that it's viewable
if(!$brand->id || $brand->template != 'brand' || !$brand->viewable()) 
  throw new Wire404Exception(); 
  • Like 1

Share this post


Link to post
Share on other sites

Thanks Ryan! 

Ultimately I concluded the same: that I need to set $config->urls->root to /admin/ in any template that makes calls like these. That worked.

Share this post


Link to post
Share on other sites

Hi,

I am having a similar problem, how did you solve this ?

 

thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By EyeDentify
      Hello Dear PW Gurus.

      Hope you fair well in these Corona Crisis times.

      Anyhow, i have a problem with a Cryptic error message that shows up when i am trying to Delete images out of a Images field.
      The Error message does say a little but it does not make sense to me why i can´t delete the images because of it.

      Is it some permission issue perhaps?

      I will attach screenshots of it and what i did prior to it.
      I am from sweden so ignore the funny words here and there.

      Hope you all can send me on the right track.
      i am running PW 3.0.139 on this install.

      Step1:

      Step 2: Select the images with the trashcan symbol

       
      Step 3: Cryptic Error message

      I am scratching my head on this one, have not seen this before.

      Thankfull for all help.
      /EyeDentify
    • By Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
    • By flydev 👊🏻
      Bootstrap-4 Minimal site profile for ProcessWire
      This profile is based on the "minimal site profile (intermediate edition)" and bundled with Boostrap v4.4.1
       
        Features
      Bootstrap SASS Font-Awesome SASS Render / helper functions for : Simple ul navigation Bootstrap Multi-level navbar Bootstrap Carousel Bootstrap Cards Bootstrap Jumbotron Boostrap Accordion Assets minification, files bundle   Dependencies
      jQuery Popper.js Bootstrap FontAwesome   Prequisites
       You'll want to install the following on your system before proceeding:
      Yarn / NPM How To Install
      Download the zip file at Github or clone directly the repo with git clone and skip the step 2. Extract the folder site-pwbs4-master into a fresh ProcessWire installation root folder. During the installation of ProcessWire, choose the profile "ProcessWire Bootstrap 4 profile".   After installation
      You can find the development file (CSS/SCSS/JS) in site/assets/dev/src
      The profile can be used as is only with $config->debug set to false. To use it in debug mode, you are required to install the dependencies with the package manager.
      Open a terminal in site/assets/dev and execute the following command-line: yarn Available commands :
      Rebuild, minify and bundle assets for release : yarn build   References
      Bootstrap v4 documentation ProcessWire documentation ProcessWire Forum: bootstrap tag ProcessWire Forum: bootstrap related posts   Credits
      The ProcessWire staff Inspiration from @gebeer and his Bootstrap 3 profile post Members who contributed in various post about Bootstrap navigation and code (see code-source for refs). Screenshots

    • By Robin S
      Add Image URLs
      Allows images/files to be added to Image/File fields by pasting URLs.

      Usage
      Install the Add Image URLs module.
      A "Paste URLs" button will be added to all image and file fields. Use the button to show a textarea where URLs may be pasted, one per line. Images/files are added when the page is saved.
       
      https://github.com/Toutouwai/AddImageUrls
      https://modules.processwire.com/modules/add-image-urls/
    • By gebeer
      Hello all,
      sharing my new module FieldtypeImageReference. It provides a configurable input field for choosing any type of image from selectable sources. Sources can be: 
      a predefined folder in site/templates/ and/or a  page (and optionally its children) and/or the page being edited and/or any page on the site CAUTION: this module is under development and not quite yet in a production-ready state. So please test it carefully.
      UPDATE: the new version v2.0.0 introduces a breaking change due to renaming the module. If you have an older version already installed, you need to uninstall it and install the latest master version.
      Module and full description can be found on github https://github.com/gebeer/FieldtypeImageReference
      Install from URL: https://github.com/gebeer/FieldtypeImageReference/archive/master.zip
      Read on for features and use cases.
      Features
      Images can be loaded from a folder inside site/templates/ or site/assets Images in that folder can be uploaded and deleted from within the inputfield Images can be loaded from other pages defined in the field settings Images can be organized into categories. Child pages of the main 'image source page' serve as categories mages can be loaded from any page on the site From the API side, images can be manipulated like native ProcessWire images (resizing, cropping etc.), even the images from a folder Image thumbnails are loaded into inputfield by ajax on demand Source images on other pages can be edited from within this field. Markup of SVG images can be rendered inline with `echo $image->svgcontent` Image names are fully searchable through the API $pages->find('fieldname.filename=xyz.png'); $pages->find('fieldname.filename%=xy.png'); Accidental image deletion is prevented. When you want to delete an image from one of the pages that hold your site-wide images, the module searches all pages that use that image. If any page contains a reference to the image you are trying to delete, deletion will be prevented. You will get an error message with links to help you edit those pages and remove references there before you can finally delete the image. This field type can be used with marcrura's Settings Factory module to store images on settings pages, which was not possible with other image field types When to use ?
      If you want to let editors choose an image from a set of images that is being used site-wide. Ideal for images that are being re-used across the site (e.g. icons, but not limited to that).
      Other than the native ProcessWire images field, the images here are not stored per page. Only references to images that live on other pages or inside a folder are stored. This has several advantages:
      one central place to organize images when images change, you only have to update them in one place. All references will be updated, too. (Provided the name of the image that has changed stays the same) Installation and setup instructions can be found on github.
      Here's how the input field looks like in the page editor:

      If you like to give it a try, I'm happy to hear your comments or suggestions for improvement. Install from URL: https://github.com/gebeer/FieldtypeImageReference/archive/master.zip
      Eventually this will go in the module directory, too. But it needs some more testing before I submit it. So I'd really appreciate your assistance.
      Thanks to all who contributed their feedback and suggestions which made this module what it is now.
       
×
×
  • Create New...