Jump to content
louisstephens

[Solved] Adding Multiple pages to Page field via API from select field

Recommended Posts

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.

47858297_ScreenShot2019-06-12at10_31_42AM.png.f3e6677b57cf8986202249a05a23b06b.png

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.

Edited by louisstephens
finally figured it out

Share this post


Link to post
Share on other sites
4 hours ago, louisstephens said:

$tags = $sanitizer->text($_POST['tags']);

My guess is that this might be the culprit. If you submit multiple tags, that likely returns something like "array-2".

You might need to do something like this:

$rawTags = $input->post->tags;
$tags = array_map(
	function($tag) use($sanitizer) {
		return $sanitizer->text($tag);
	},
	is_array($rawTags) ? $rawTags : [$rawTags]
);

 

Share this post


Link to post
Share on other sites

Thanks Bitpoet. I gave it a shot and I am getting: 

#0 /MAMP/htdocs/development/wire/core/Selectors.php(460): ProcessWire\Selectors->create('hello', '', '')
#1 /MAMP/htdocs/development/wire/core/Selectors.php(142): ProcessWire\Selectors->extractString('the-4th-of-july')
#2 /MAMP/htdocs/development/wire/core/Selectors.php(128): ProcessWire\Selectors->setSelectorString('hello,the-4th-o...')
#3 /MAMP/htdocs/development/wire/core/PagesLoader.php(221): ProcessWire\Selectors->init('hello,the-4th-o...')
#4 /MAMP/htdocs/development/wire/core/Pages.php(246): ProcessWire\PagesLoader->find('hello,the-4th-o...', Array)
#5 /MAMP/htdocs/development/wire/core/Wire.php(386): ProcessWire\Pages->___find('hello,the-4th-o...', Array)
#6 /MAMP/htdocs/development/wire/core/WireHooks.php(733): ProcessWire\Wire->_callMethod('___find', Array)
#7 /MAMP/htdocs/development/wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\Pages), 'find', Array)
#8 /MAMP/htdocs/development/wire/core/PagesLoader.php(430): ProcessWire\Wire->__call('find', Array)
#9 /MAMP/htdocs/development/wire/core/Pages.php(390): ProcessWire\PagesLoader->get('hello,the-4th-o...', Array)
#10 /MAMP/htdocs/development/wire/modules/Fieldtype/FieldtypePage.module(700): ProcessWire\Pages->get('hello,the-4th-o...')
#11 /MAMP/htdocs/development/wire/modules/Fieldtype/FieldtypePage.module(563): ProcessWire\FieldtypePage->sanitizeValuePageArray(Object(ProcessWire\Page), Object(ProcessWire\Field), Array)
#12 /MAMP/htdocs/development/wire/core/Page.php(1072): ProcessWire\FieldtypePage->sanitizeValue(Object(ProcessWire\Page), Object(ProcessWire\Field), Array)
#13 /MAMP/htdocs/development/wire/core/Page.php(932): ProcessWire\Page->setFieldValue('tags', Array, true)
#14 /MAMP/htdocs/development/wire/core/Page.php(1825): ProcessWire\Page->set('tags', Array)
#15 /MAMP/htdocs/development/site/templates/ajax.php(64): ProcessWire\Page->__set('tags', Array)
#16 /MAMP/htdocs/development/wire/core/TemplateFile.php(287): require('/Volumes/ArtDep...')
#17 /MAMP/htdocs/development/wire/core/Wire.php(380): ProcessWire\TemplateFile->___render()
#18 /MAMP/htdocs/development/wire/core/WireHooks.php(733): ProcessWire\Wire->_callMethod('___render', Array)
#19 /MAMP/htdocs/development/wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\TemplateFile), 'render', Array)
#20 /MAMP/htdocs/development/wire/modules/PageRender.module(514): ProcessWire\Wire->__call('render', Array)
#21 /MAMP/htdocs/development/wire/core/Wire.php(383): ProcessWire\PageRender->___renderPage(Object(ProcessWire\HookEvent))
#22 /MAMP/htdocs/development/wire/core/WireHooks.php(733): ProcessWire\Wire->_callMethod('___renderPage', Array)
#23 /MAMP/htdocs/development/wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\PageRender), 'renderPage', Array)
#24 /MAMP/htdocs/development/wire/core/WireHooks.php(834): ProcessWire\Wire->__call('renderPage', Array)
#25 /MAMP/htdocs/development/wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\Page), 'render', Array)
#26 /MAMP/htdocs/development/wire/modules/Process/ProcessPageView.module(206): ProcessWire\Wire->__call('render', Array)
#27 /MAMP/htdocs/development/wire/core/Wire.php(383): ProcessWire\ProcessPageView->___execute(true)
#28 /MAMP/htdocs/development/wire/core/WireHooks.php(733): ProcessWire\Wire->_callMethod('___execute', Array)
#29 /MAMP/htdocs/development/wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\ProcessPageView), 'execute', Array)
#30 /MAMP/htdocs/development/index.php(55): ProcessWire\Wire->__call('execute', Array)
#31 {main}

I guess I shouldnt be tryping to still pass $tags the same way I was with $p->tags = $tags? Sorry, I have never used array_map before so I wasnt sure of the desired output etc. I do appreciate the help

Share this post


Link to post
Share on other sites
10 hours ago, louisstephens said:

I guess I shouldnt be tryping to still pass $tags the same way I was with $p->tags = $tags?

Yes, loop over $tags and call $page->tags->add() for each like in the snippet you posted. Of course, you can just call $sanitizer->text for every $tag inside the loop in your third code snippet and go without the array_map. array_map is just a function that calls the passed function for every entry in the array given to it and returns a new array with the results.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks again @BitPoet, I moved everything out into a new template so I could just test out the page reference field, and for some reason this is still blowing up in my face. I dont get the error like before, but it doesnt seem like anything is working. I simplified my php to:

$rawTags = $input->post->tags;
$tags = array_map(
	function($tag) use($sanitizer) {
		return $sanitizer->text($tag);
	},
	is_array($rawTags) ? $rawTags : [$rawTags]
);

$p = $pages->get(1092);
$p->of(false);

foreach($tags as $tag) {
	$p->tags->add($tag);
	bdump($tag);
}
$p->save();

The bdump shows that $tag = "2018,2019" (which is what I selected from the options field on the frontend", but it does not seem like it is actually adding anything to the field in the backend. 😓

Share this post


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

The bdump shows that $tag = "2018,2019"

Are these page names or page ids? For $p->tags->add to work, anything numeric must be a valid page id. And, duh, I have to say sorry. I just took a look into PageArray.php and saw that you can actually pass an array of pages to PageArray::add and it will call itself recursively, thought the same rules apply then. If it looks numeric, it needs to be a page id.

Share this post


Link to post
Share on other sites

Ah, they are indeed page names:

<select id="select_tags" name="select_tags" multiple="multiple">
	<?php $tags = $pages->find("template=tag"); ?>
    	<option value="">Select Your Tags</option>
    <?php foreach ($tags as $tag) : ?>
        <option value="<?= $tag->name; ?>"><?= $tag->title; ?></option>
	<?php endforeach; ?>
</select>

And my Tag structure is set up like:

Tags
-Years
--2018
--2019
-Months
--January
--February
--Etc

Sorry, I should have posted that earlier in my original post. So the url for the tag "2018" looks like "/tags/years/2018/" . I did change the output to be "ids" and then tried it out and it still isnt saving them. I really appreciate your help with this. If it is indeed recursive, I have no idea why " $p->tags->add($rawTags);" want work.

Share this post


Link to post
Share on other sites

Well, a little bit of an update. I just saw in my logs that I am getting an exception: "Page 1102 is not valid for tags ( Page 1102 does not have required parent 1015)"..

I am wondering if my tag structure is messing up the Page field somehow as my select field on the front end is really only displaying:

<select id="select_tags" name="select_tags" multiple="multiple">
	<option value="">Select Your Tags</option>
	<option value="1102">2018</option>
	<option value="1104">2019</option>
	<option value="1105">January</option>
	<option value="1106">February</option>
	<option value="1109">March</option>
	<option value="1110">April</option>
	<option value="1111">May</option>
</select>

I "omitted" the "Years", "Months" etc as I have them set to the template "parent-tag" while the actual "2019, January" are set to the template "tags".. I tried the "fix" from the following, but that doesnt seem to solve the issue either. It is odd to me that I can select via the admin and everything works fine.

**Update**

Using

$rawTags = $input->post->tags;

$p = $pages->get(1092);
$p->of(false);
bdump($rawTags);
$p->dev_test->add($rawTags); //created a new page field without a "parent", just set the template

This seems to add 1 page at a time now. However, it will not add multiple. I even tried to foreach through them to no avail.. Page fields obviously hate me today.

Share this post


Link to post
Share on other sites

I don't mean to keep this post going too long, but I am at a complete loss here and have no clue what else to do:

$rawTags = $input->post->tags;
tags = array_map(
	function($tag) use($sanitizer) {
    	return $sanitizer->text($tag);
	},
    is_array($rawTags) ? $rawTags : [$rawTags]
);

bdump($tags);
bdump($rawTags);

$p = $pages->get(1092);
$p->of(false);

foreach($tags as $tag) {
	echo $tag;
	$p->tags->add($tag); // add another page by id
}
$p->save();

The above will only add one item even though I am foreach'ing through the array and the array (in testing) has 2 items in it. It also only works when 1 item is selected. If I select 2 items from the options field, it will not add either of the items. Is there a fundamental piece I am missing here, or am I going about this the completely wrong way? I am at my wits end with this🤬

 

I did find 

so I tried 

$p = $pages->get(1092);
$p->of(false);
$p->dev_test->add(array($tags));
bdump($tags);
//add(array(1023,1026))
//bdump($Test->id);
$p->save();

It works if just input 1102,1103 directly into the array like ->add(array(1102,1103)) , but not by passing my array to it.

Share this post


Link to post
Share on other sites

Well, I was really dumb here. All it took was me taking a break and coming back with some fresh eyes. After another failed attempt to get it working, I decided to bdump $rawTags and $tags and then it hit me. 

$rawTags bdump: (which were the select values posted by the form): "1102,1103,1104,1105"

$tags bdump (what BitPoet wrote me): array(1) 0 => "1102,1103,1104,1105"

I was essentially passing an array with 1 item with all the ids. After using

$tagsArray = explode(',', $rawTags);

I get: array(4) 0=>"1102"  1=>"1103" 2=>"1104" 3=>"1105" and then it succesfully works with ->add(array($tagsArray)); 

I appreciate everyone's help with this and patience. I can finally put this to bed.

  • Like 1

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 MateThemes
      Hello everyone!
      I am working with Processwire since some time. But some topics are quite hard for me.
      I have a Portfolio (Gallery) Page.
      I am build a template with Portfolio Index and pages with portfolio entries.
      Structure:
      Portfolio Index
      -- Portfolio Entry
      -- Portfolio Entry
      and so on.
      Portfolio Entry has an Image field with max 12 images and are accessible Templates. 
      Now I want to display the single Portfolio Entry on the Portfolio Index and Paginate them. In the index page all images of a single Entry page should be displayed (I should not be organized as albums, where a random image of the portfolio entry should be displayed). 
      I have no clue to achieve this. May someone could give me an advice.
      Thank you in advance!
    • By ngrmm
      I have a page with a table. Each table row has a page-reference field and a checkbox.
      The Page sends emails to all users (page-refrence->email-field) and change the value of the checkbox in a row to 1.
      It works with this:
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?> But i have to use a variable in my emailbody.inc which i'm able to get in the table-loop.
      So i do the including of the body inside my loop. But this doesn't work anymore. Page sends out the emails but is unable to change the value of the checkbox.
      I get no errors!
      I'm using ProTable
      <?php // event ID fron url query $eventID = $input->get('eventID','int'); // get event-page $event = $pages->get($eventID); // config $fromEmail = $event->event_mail_from; $fromName = $event->event_mail_from_name; $emailSubject = $event->event_subject; // loop through table and send out emails foreach($event->event_clients_list as $event_table_row) { // get client page $clientPage = $event_table_row->client_name; // get client email $clientEmail = $clientPage->email; // email html body ob_start(); include('./_inc/emailbody.inc'); $emailBody = ob_get_clean(); // make event-page editable $event->of(false); // if client isn't invited yet (checkbox not checked) if($event_table_row->client_invited == '') { // send email $m = new WireMail(); $m->to($clientEmail); $m->from($fromEmail, $fromName); $m->subject($emailSubject); $m->bodyHTML($emailBody); $m->send(); // mark client as invited $event_table_row->client_invited = 1; $event->save('event_clients_list'); } } ?>  
    • By Vigilante
      In general, is there an easy way to know which method should be used to access the API?
      For example, when _ini.php is used in the theme, it would seem you have to use wire()->addHookBefore(...). But on other sites where they used ready.php, I've seen it go straight to just doing $this->addHookAfter() even though there is no class or namespace set up in the ready.php file.
      So how do I know when I can do $this, or $wire, or wire() to access things? And any other variables I'm not aware of.
      Thanks!
    • By gerritvanaaken
      My client could save some time if she could select multiple pages at once. Of yourse, we are using the standard page selector in multi-tree mode, but she has to select one page after another. Each time she has to open the site tree again and open the required subtree, finding the page she wants to select.
      Would be cool to let the tree open after selecting a page, so she could select multiple pages in one "tree session". Understand what I mean? Anybody any solution to this?
      Alternatively, maybe the site tree could keep its state with all the subtrees open, when you are re-open it again.
    • By VeiJari
      Hello forum,
      This is really a weird one, because front end editing works in a earlier website we did to a customer. 
      When I check the source code for current website it does initiate front end edit: 
      <span id=pw-edit-1 class='pw-edit pw-edit-InputfieldPageTitle' data-name=title data-page=1021 data-lang='1017' style='position:relative'><span class=pw-edit-orig>Tekijät</span><span class=pw-edit-copy id=pw-editor-title-1021 style='display:none;-webkit-user-select:text;user- select:text;' contenteditable>Tekijät</span></span>  But when I double click nothing happens (yes I'm 100% sure I'm superuser and logged in)
      I also tried to apply the front end with other methods than:
      $page->edit('title'); But didn't work either.
      We are using jquery 2.2.4, so it should not be a problem.
      Is this a bug related to current master or something else?
      Someone else having this problem as well?
×
×
  • Create New...