Jump to content
Harmen

Can't add pages to Page field [Asmselect]

Recommended Posts

I want to add a few pages to an AsmSelect Page field inside a repeater using the following code:

$trialsPage = wire("pages")->get(28422); // Get the page
$trialsPage->of(false);
$newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater
foreach ($selectedProducts as $selectedProduct){
	$productPage = $pages->get("template=product, reference=$selectedProduct");
	$newTrial->trial_selected_products->add($productPage);
}
$newTrial->save();
$trialsPage->save();

However, when I check the page where the field is located it doesn't have the new values as expected. The selected pages exist, the field is in the right location, made sure that the output formatting is turned off: $page->of(false); But it still doesn't work with a variable. No matter what I try, it doesn't work.

It only works when I replace $selectedProduct with a hardcoded string. Am I doing something wrong here?

Share this post


Link to post
Share on other sites

Does it work with $selectedProduct in curly brackets like this {$selectedProduct}?

$trialsPage = wire("pages")->get(28422); // Get the page
$trialsPage->of(false);
$newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater
foreach ($selectedProducts as $selectedProduct){
	$productPage = $pages->get("template=product, reference={$selectedProduct}");
	$newTrial->trial_selected_products->add($productPage);
}
$newTrial->save();
$trialsPage->save();

 

Share this post


Link to post
Share on other sites

And another thing with repeaters via API. At least I save after each new repeater - as seen below.

$trialsPage = wire("pages")->get(28422); // Get the page
$trialsPage->of(false);
$newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater
foreach ($selectedProducts as $selectedProduct){
	$productPage = $pages->get("template=product, reference={$selectedProduct}");
	$newTrial->trial_selected_products->add($productPage);
	$newTrial->save();
}
$trialsPage->save();

 

Share this post


Link to post
Share on other sites
14 minutes ago, wbmnfktr said:

Does it work with $selectedProduct in curly brackets like this {$selectedProduct}?

Tried that but doesn't work.

 

9 minutes ago, wbmnfktr said:

At least I save after each new repeater - as seen below.

Doesn't fix the problem either.

 

I have no problems editing the other fields in the repeater, it's just the AsmSelect Page field

Share this post


Link to post
Share on other sites

Ok... just some other thoughts.

Is that page field setup correctly?

Are the pages you want to add matching the parent/template settings within the page field?

Are those pages you want to add hidden or unpublished?

Does your query find those pages?

 

 

Share this post


Link to post
Share on other sites
12 minutes ago, wbmnfktr said:

Is that page field setup correctly?

Yes, checked everything a couple times. Don't know why I can't set the values

12 minutes ago, wbmnfktr said:

Are the pages you want to add matching the parent/template settings within the page field?

Yes

12 minutes ago, wbmnfktr said:

Are those pages you want to add hidden or unpublished?

No, they are published and not hidden

12 minutes ago, wbmnfktr said:

Does your query find those pages?

Yes

Can it be a problem that the page with this field has a template without a file?

Share this post


Link to post
Share on other sites
3 minutes ago, wbmnfktr said:

I guess it's something totally obvious but right now I'm out off new ideas. Sorry.

That's okay. It's weird but I can't figure it out either

Share this post


Link to post
Share on other sites

Did you try (shouldn't be necessary, though)

$productPage = $pages->get("template=product, reference={$selectedProduct->id}");

 

Share this post


Link to post
Share on other sites

What sort of field is "reference"? What sort of data is $selectedProduct?

14 hours ago, Harmen said:

It only works when I replace $selectedProduct with a hardcoded string.

If that is so then it indicates that the data held in $selectedProduct is not the same as the data in your hardcoded string.

Tracy Debugger is your friend here...

$trialsPage = wire("pages")->get(28422); // Get the page
$trialsPage->of(false);
$newTrial = $ordersPage->trial_repeater_orders->getNewItem(); // Add item to repeater
foreach ($selectedProducts as $selectedProduct){
	// See what is in $selectedProduct
	bd($selectedProduct, 'selectedProduct');
	$productPage = $pages->get("template=product, reference=$selectedProduct");
	// Check that $productPage is not a NullPage (i.e. no matching page found)
	// and that is the right kind of page (template, parent, etc) to add to trial_selected_products
	bd($productPage, 'productPage');
	$newTrial->trial_selected_products->add($productPage);
}
$newTrial->save();
$trialsPage->save();

 

  • Like 1

Share this post


Link to post
Share on other sites
18 hours ago, Autofahrn said:

Did you try (shouldn't be necessary, though)


$productPage = $pages->get("template=product, reference={$selectedProduct->id}");

 

Doesn't work, $selectedProduct isn't a Page object but a string.

--

7 hours ago, Robin S said:

What sort of field is "reference"? What sort of data is $selectedProduct?

The reference field is just a Text field. $selectedProduct is a string. The reference field holds the reference of the product which is unique for each product. 

Used TracyDebugger, and this are the results:

Quote

bd($selectedProduct, 'selectedProduct');

This returns a string, the exact same string that the product has stored in the reference field.

Quote

bd($productPage, 'productPage');

This returns a Page Object, containing the actual page that I want to add to the AsmSelect Page field.

Conclusion: $productPage contains an actual page that exists, but for some reason I cannot add this page to the AsmSelect Page Field.

Did some more digging and found the following:

There are 7 categories: each containing a different kind of products. I have no problem with adding the products of the first 4 categories, but for the remaining 3 categories it doesn't work, a hardcode string for these pages doesn't help either, even though I can find the pages manually in the selector.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By louisstephens
      Going through my long quest to get better with ajax and utilizing the api, I have hit yet another roadblock. I currently have a form with an image field (thanks to flydev for getting that sorted), "title" text input, and a select field set to multiple. In my ajax call, I added in:
      tags = $("#select-tags").val(); form_data.append('tags', tags); $.ajax({ type: 'POST', data: form_data, contentType: false, processData: false, url: '/ajax/upload-preview/', success: function(data) { console.log("Woo"); }, error: function(xhr, ajaxOptions, thrownError) { alert(xhr.responseText); } }); And in the ajax template: 
      $tags = $sanitizer->text($_POST['tags']); $image = $sanitizer->text($_POST['image']); $p = new Page(); $p->template = "preview"; $p->parent = $pages->get("/previews/"); $p->name = $title; $p->title = $title; $p->tags = $tags; $p->save(); If I select a "tag" from the select input and submit, it does indeed add it to the Page Reference field in the backend. However, this does not work with an array being passed to it of multiple options.

      So it does appear that my ajax call is trying to submit multiple options, but I am really just unsure how to get these two added in. I saw in other forums posts of add($page) and even add(array()). Do I need to handle this js array differently or do  I need to foreach through the $tags to add it like:
      foreach($tags as $tag) { $p->tags->add($tag); $p->save(); } I tried this approach, but apparently I am still missing something.
       
      Edit:
      I was doing some tweaking, and I know I can split the js array out like:
      for (i = 0, len = tags.length; i < len; i++) { console.log(tags[i]); } However, I am not sure then how to handle the POST in php if I were to split it out.
    • By louisstephens
      I have been messing around with creating pages from ajax requests, and it has gone swimmingly thus far. However, I am really struggling with creating a page and saving an image via ajax. 
      The form:
      <form action="./" role="form" method="post" enctype="multipart/form-data"> <div> <input type="text" id="preview" name="preview" placeholder="Image Title"> </div> <div> <input type="file" id="preview-name" name="preview-name"> </div> <div> <select id="select-tags" name="select-tags"> <?php $tags = $pages->find("template=tag"); ?> <option value="">Select Your Tags</option> <?php foreach ($tags as $tag) : ?> <option value="<?= $tag->name; ?>"><?= $tag->name; ?></option> <?php endforeach; ?> </select> </div> <div> <button type="button" id="submit-preview" name="submit" class="">Upload Images</button> </div> </form>  
      The ajax in my home template:
      $('#submit-preview').click(function(e) { e.preventDefault(); title = $("#preview").val(); image = $("input[name=preview-name]"); console.log(title); console.log(image); data = { title: title, image: image //not sure if this is actually needed }; $.ajax({ type: 'POST', data: data, url: '/development/upload-preview/', success: function(data) { console.log("Woo"); }, error: function(xhr, ajaxOptions, thrownError) { alert(xhr.responseText); } }); }); And finally in my ajax template:
      $imagePath = $config->paths->assets . "files/pdfs/"; //was from an older iteration $title = $sanitizer->text($_POST['title']); $image = $sanitizer->text($_POST['image']); $p = new Page(); $p->template = "preview"; $p->parent = $pages->get("/previews/"); $p->name = $title; $p->title = $title; $p->save(); $p->setOutputFormatting(false); $u = new WireUpload('preview_image'); $u->setMaxFiles(1); $u->setOverwrite(false); $u->setDestinationPath($p->preview_image->path()); $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png', 'pdf')); foreach($u->execute() as $filename) { $p->preview_image->add($filename); } $p->save(); I can complete the file upload but just using a simple post to the same page and it it works well, but I was really trying to work out the ajax on this so I could utilize some modals for success on creation (and to keep my templates a little cleaner). When I do run the code I have, a new/blank folder is created under assets, and a new page is created with the correct title entered. However, no image is being processed. I do get a 200 status in my console. I have searched google for help, but everything seems to be slightly off from my needs. If anyone could help point me in the right direction I would greatly appreciate it. 
    • By louisstephens
      This might be a completely dumb question, but I cant seem to wrap my head around it. I have a page reference field that allows users to select "Tags". In the front end I would like to use the titles as class names for a single item. ie:
      <?php $previews = $pages->find("template=preview"); ?> <?php foreach($previews as $preview): ?> <div class="tagOne TagTwo tagThree"> <!-- use another foreach to output--> <img src="<?=$preview->preview_image->url; ?>" /> </div> <?php endforeach; ?> I am little stumped as I know I need a foreach loop to produce each tag title, but how do I insert them all into one corresponding div class with spaces?
      Whelp, that was the easiest thing, but my brain just didnt "get it". Just put the foreach in the "class" inside of the overall foreach. Ugh 😓
    • By schwarzdesign
      We recently rebuilt the Architekturführer Köln (architectural guide Cologne) as a mobile-first JavaScript web app, powered by VueJS in the frontend and ProcessWire in the backend. Concept, design and implementation by schwarzdesign!
      The Architekturführer Köln is a guidebook and now a web application about architectural highlights in Cologne, Germany. It contains detailled information about around 100 objects (architectural landmarks) in Cologne. The web app offers multiple ways to search through all available objects, including:
      An interactive live map A list of object near the user's location Filtering based on architect, district and category Favourites saved by the user The frontend is written entirely in JavaScript, with the data coming from a ProcessWire-powered API-first backend.
      Frontend
      The app is built with the Vue framework and compiled with Webpack 4. As a learning exercise and for greater customizability we opted to not use Vue CLI, and instead wrote our own Webpack config with individually defined dependencies.
      The site is a SPA (Single Page Application), which means all internal links are intercepted by the Vue app and the corresponding routes (pages) are generated by the framework directly in the browser, using data retrieved from the API. It's also a PWA (Progressive Web App), the main feature of which is that you can install it to your home screen on your phone and launch it from there like a regular app. It also includes a service worker which catches requests to the API and returns cached responses when the network is not available. The Architekturführer is supposed to be taken with you on a walk through the city, and will keep working even if you are completely offline.
      Notable mentions from the tech stack:
      Vue Vue Router for the SPA functionality VueX for state management and storage / caching of the data returned through the API Leaflet (with Mapbox tiles) for the interactive maps Webpack 4 for compilation of the app into a single distributable Babel for transpilation of ES6+ SASS & PostCSS with Autoprefixer as a convenience for SASS in SFCs Google Workbox to generate the service worker instead of writing lots of boilerplate code Bootstrap 4 is barely used here, but we still included it's reboot and grid system Backend
      The ProcessWire backend is API-only, there are no server-side rendered templates, which means the only PHP template is the one used for the API. For this API, we used a single content type (template) with a couple of pre-defined endpoints (url segments); most importantly we built entdpoints to get a list of all objects (either including the full data, or only the data necessary to show teaser tiles), as well as individual objects and taxonomies. The API template which acts as a controller contains all the necessary switches and selectors to serve the correct response in <100 lines of code.
      Since we wanted some flexibility regarding the format in which different fields were transmitted over the api, we wrote a function to extract arbitrary page fields from ProcessWire pages and return them as serializable standard objects. There's also a function that takes a Pageimage object, creates multiple variants in different sizes and returns an object containing their base path and an array of variants (identified by their basename and width). We use that one to generate responsive images in the frontend. Check out the code for both functions in this gist.
      We used native ProcessWire data wherever possible, so as to not duplicate that work in the frontend app. For example:
      Page names from the backend translate to URLs in the frontend in the form of route parameters for the Vue Router Page IDs from ProcessWire are included in the API responses, we use those to identify objects across the app, for example to store the user's favourites, and as render keys for object lists Taxonomies have their own API endpoints, and objects contain their taxonomies only as IDs (in the same way ProcessWire uses Page References) Finally, the raw JSON data is cached using the cache API and this handy trick by @LostKobrakai to store raw JSON strings over the cache API.
      Screenshots














    • By Liam88
      Hi All,
      New user over at Processwire and have been rebuilding my site based on this CMS.
      I have been able to find so many answers through Google but I'm a little stuck on this one.
      I have my services page -> services categories -> category children.
      An example of those would be - domain -> services -> ppc -> management
      I also have a set of tags which have different names - services-tag -> grow-your-traffic
      Under these tags I would have multiple links to pages such as ppc, seo, social media and so on.
      A second example would be - services-tag -> convert-your-traffic
      Under here i would have multiple links to pages such as CRO
      Now the set of tags are not visible on-site as they are only created to give overview content to the main services categories.
      Using the categories and the tags I am looking to produce a layout such as (i have also attached an image as an example:
      Tag_1 headline
      Pull all services categories linking to Tag_1
      Tag_1 snippet
      Tag_2 headline
      Pull all services categories linking to Tag_2
      Tag_2 snippet
      So far I have this snippet which is pulling in the tag content but unable to get the posts to show under each of the tags. 
      If i change the if and statement to "tags" instead of "tag" then all posts show under all tags. Where as i want it to show only the posts which are linked to that tag.
      <?php namespace ProcessWire; $tags = $pages->get("/categories-services/")->children(); // Gets the tags $posts = $pages->get("/services/")->children(); // Gets the services categories $link = $tags->ref_6; // Gets the tags and services categories link - under here you have pages_id (services cat id) and data (tags id) // Tag header and summary foreach($tags as $tag) { // This breaks down the tags into sections echo '<section id="services"> <div class="container"> <div class="row"> <h2 class="heading-1"><span>'. $tag->headline.'</span></h2> <p class="mb-5">'. $tag->summary.'</p> </div> <div class="row justify-content-around services">'; // Main services categories that link to the above tags if ($posts->id === $link->pages_id && $tag->id === $link->data){ foreach($posts as $service){ // This pulls in the services categories under the tag header. echo '<div class="card flex-card" id=""> <div class="card-img"> <a href="/'. $service->name.'" title="'. $service->name .'"> <img class="card-img-top" src="../assets/files/'. $service->id.'/'. $service->img_1.'" alt="'. $service->img_1.'" title="'. $service->img_1.'"></a> </div> <div class="card-body"> <h3 class="card-title">'. $service->headline.'</h3> <p class="card-text">'. $service->summary .'</p> <div class="card-action"> <a href="" title="'. $service->name .'" role="link" class="link">View service<span></span></a> </div> </div> </div> '; } } // Grey snippet text echo '</div> </div> </section> <div class="snip-2 light-grey"> <div class="container"> <div class="row text-center">'. $tag->get('grey') .'</div> </div> </div>'; } ?> I appreciate this is a long post but i'm trying to be clear as I appreciate everyone's time.
      Any insight into where I am going wrong is greatly appreciated.
      Liam



×
×
  • Create New...