Jump to content

Can't add pages to Page field [Asmselect]


Harmen
 Share

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?

Link to comment
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();

 

Link to comment
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();

 

Link to comment
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

Link to comment
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?

Link to comment
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
Link to comment
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.

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      Select Images
      An inputfield that allows the visual selection and sorting of images, intended for use with the FieldtypeDynamicOptions module. Together these modules can be used to create a kind of "image reference" field.

      Integration with FieldtypeDynamicOptions
      InputfieldSelectImages was developed to be used together with FieldtypeDynamicOptions (v0.1.3 or newer):
      Create a Dynamic Options field. Choose "Select Images" as the "Inputfield type". Select Images appears in the "Multiple item selection" category but you can set "Maximum number of items" to 1 if you want to use Select Images for single image selections. Define selectable options for the field via a FieldtypeDynamicOptions::getSelectableOptions hook. See some examples below. FieldtypeDynamicOptions is recommended but is not a strict requirement for installing InputfieldSelectImages in case you want to use an alternative way to store the field data.
      Selection of Pageimages
      In this example the field allows selection of Pageimages that are in the "images" field of the home page.
      The field will store URLs to the Pageimages so it works as a kind of "image reference" field. You can use the "Format as Pagefile/Pageimage object(s)" option for the Dynamic Options field to have the formatted value of the field be automatically converted from the stored Pageimage URLs to Pageimage objects.
      $wire->addHookAfter('FieldtypeDynamicOptions::getSelectableOptions', function(HookEvent $event) { // The page being edited $page = $event->arguments(0); // The Dynamic Options field $field = $event->arguments(1); // For a field named "select_images" if($field->name === 'select_images') { $options = []; // Get Pageimages within the "images" field on the home page foreach($event->wire()->pages(1)->images as $image) { // Add an option for each Pageimage // When the key is a Pageimage URL the inputfield will automatically create a thumbnail // In this example the label includes the basename and the filesize /** @var Pageimage $image */ $options[$image->url] = "{$image->basename}<br>{$image->filesizeStr}"; } $event->return = $options; } }); Selection of image files not associated with a Page
      When not working with Pageimages you must add a "data-thumb" attribute for each selectable option which contains a URL to a thumbnail/image.
      In this example the field allows selection of image files in a "/pics/" folder which is in the site root.
      $wire->addHookAfter('FieldtypeDynamicOptions::getSelectableOptions', function(HookEvent $event) { // The page being edited $page = $event->arguments(0); // The Dynamic Options field $field = $event->arguments(1); // For a field named "select_images" if($field->name === 'select_images') { $options = []; // Get files that are in the /pics/ folder $root = $event->wire()->config->paths->root; $path = $root . 'pics/'; $files = $event->wire()->files->find($path); // Add an option for each file foreach($files as $file) { $basename = str_replace($path, '', $file); $url = str_replace($root, '/', $file); // The value must be an array with the following structure... $options[$url] = [ // The label for the image 'label' => $basename, 'attributes' => [ // An image URL in the "data-thumb" attribute 'data-thumb' => $url, ], ]; } $event->return = $options; } }); The field values don't have to be image URLs
      The values stored by the Dynamic Options field don't have to be image URLs. For example, you could use the images to represent different layout options for a page, or to represent widgets that will be inserted on the page.
      Also, you can use external URLs for the thumbnails. In the example below the options "calm" and "crazy" are represented by thumbnails from placecage.com.
      $wire->addHookAfter('FieldtypeDynamicOptions::getSelectableOptions', function(HookEvent $event) { // The page being edited $page = $event->arguments(0); // The Dynamic Options field $field = $event->arguments(1); // For a field named "calm_or_crazy" if($field->name === 'calm_or_crazy') { $options = []; // Add options that are illustrated with thumbnails from placecage.com $options['calm'] = [ // The label for the option 'label' => 'Nicolas Cage is a calm man', 'attributes' => [ // An image URL in the "data-thumb" attribute 'data-thumb' => 'https://www.placecage.com/260/260', ] ]; $options['crazy'] = [ // The label for the option 'label' => 'Nicolas Cage is a crazy man', 'attributes' => [ // An image URL in the "data-thumb" attribute 'data-thumb' => 'https://www.placecage.com/c/260/260', ] ]; $event->return = $options; } }); Field configuration
      You can define labels for the button, notices, etc, that are used within the inputfield if the defaults don't suit.

       
      https://github.com/Toutouwai/InputfieldSelectImages
      https://processwire.com/modules/inputfield-select-images/
    • By Saleena Jhon
      Hello There, I have saw a post that was covering event-calendar with php, ajax and js. That was showing a monthly overview when I click on a "month" button or when I switch the month. And show the events on one particular date when I pick a day. Also, most events are kind of exhibitions and so they have a start date and an end date much later, and occur on each day in-between as well. So on the template I put two date picking fileds date_start and date_end. Is there an elegant way to select the events using the API? If yes, kindly help me out.
      Thanks in Advance
      Regards: 
       
    • By opalepatrick
      I am working on my first Process Module. I am creating forms. Fairly straightforward. However, I really can't work out how to create multiple fieldsets?
      $fieldset = $this->modules->get('InputfieldFieldset'); $fieldset->label = 'Customer Source'; $field = $this->modules->get('InputfieldPage'); $field->inputfield = 'InputfieldSelect'; $field->findPagesSelector = 'parent_id=1449, include=hidden'; $field->labelFieldName = 'yff-lead'; $field->name = 'yfflead'; $field->columnWidth = 16; $fieldset->add($field); $field = $this->modules->get('InputfieldPage'); $field->inputfield = 'InputfieldSelect'; $field->findPagesSelector = 'parent_id=1452, include=hidden'; $field->labelFieldName = 'customer-type'; $field->name = 'customertype'; $field->columnWidth = 16; $fieldset->add($field); //Rinse and Repeat $fieldset->label = 'Contacts'; $field = $this->modules->get('InputfieldPage'); $field->inputfield = 'InputfieldSelect'; $field->findPagesSelector = 'parent_id=1538, include=hidden'; $field->labelFieldName = 'salutation'; $field->name = 'salutation'; $field->columnWidth = 16; $fieldset->add($field); I can create the first fieldset (Customer Source) but then get into trouble as the second fieldset overwrites the first. I understand why, but trying to use the open and close fieldset routine has flummoxed me. Any help appreciated.
    • By Neue Rituale
      https://github.com/neuerituale/FieldtypeOembed
      or in the modules directory: https://processwire.com/modules/fieldtype-oembed/
    • By VeiJari
      Hello forum, we're trying to use Processwire as our REST-API. We are having problems with our API login to Processwire from frontend. It gives us 403 error.
      We have installed ProcessWire to subdirectory (/api/*) and our frontend is static JS files at root ( / ). Apache access logs gives 404 to our POST-request, but browser devtools shows 403 for our POST /api/login request. 
      Processwire backend panel works. We also have a GET endpoint for the API that returns 200 with correct payload.  So we're wondering why does our GET works but POST doesn't?
      Does this have something to do with Processwire .htaccess, or is this because of our webhost? What should we check first? Any help would be appreciated.
×
×
  • Create New...