Jump to content

Add to pagefield after creation


mel47
 Share

Recommended Posts

Hi

I'm now understanding the basic of PW but for this one, I have no idea where to begin.

I have 2 pages : publication and member. I want to add publications (automatically as I created them) to its member. Since I have hundred of publications, I want to create them using csv import. I used a specialised software to export them in the way I want. The format would be

Name LastName1, Name LastName2 (2016) Title. Journal. Vol:Number. Url.

I have variable numbers of authors by publication. My member's template have the title containing "Name LastName" and have a pageField "publi".

How I can achieve that? I read in the forums I could assign page to a field via hooks. But I'm not sure what I should do.

Also a more design question, do I should create all authors as unique fields? How I can create the good number of those fields? Or can I just create 1 author field in publi's template and search inside this value? Also for the others fields, do I should create them individually if I want to output this specific markup (italic, bold)?

Hope it is clear... Thanks

Mel

Link to comment
Share on other sites

Hi Mel,

I'm not sure I understand your intention in full.
Assuming that "members" and "authors" are different entities, I would create templates for members, authors and publications.
The publication template gets a multiselect pagefield to the authors, so you easily cover the case of more than one author of a publication.
And yes, the members template contains a pagefield to the publications (multiselect too).
What about the markup: That's a question of the template files, not of the templates.

 

Link to comment
Share on other sites

Hum, sounds interesting to get authors as pagefields. I suppose I can do find this template for each member to get them displayed. So I don't need a publication field in member? Still have to see if it's possible to import, will try later this weekend.

For the markup, I know, but I'm asking because I'm still unable to import html markup through CSV import. So I'm not sure what to do in that case.

thanks

Mel

Link to comment
Share on other sites

I'm stuck for 2 days to search how to output this pagefield.

So

Member (template)

Publication(template). Contains a MultiPagefield called "Author".

 

I want to output in template.php

//stuff from member template (works fine)

  // get publication from this author
  $author = $page->find("template=member, title=$page");
   $publi = $pages->find("template=publication, author=$author"); //doesn't work
  
   foreach($publi as $p) {
      $out .=  "<li>{$p->title}</li>";
     }

 

Where is my error? Do I have to add a children somewhere to get all pages from pagefield?

Thanks

Mel

Link to comment
Share on other sites

Could you reveal more details about your templates? (Their fields, etc.)

And what is the structure of the CSV file? (First line of column titles, and a sample of the following lines)
 

Edit:

Quote

$author = $page->find("

Wasn't that intended to be

$author = $pages->find("
  • Like 1
Link to comment
Share on other sites

Thanks everyone.

The simplest solution works fine (with the "s" in pages!):

$author = $pages->find("template=member, title=$page");
$publi = $pages->find("template=publication, author=$page");

 

Link to comment
Share on other sites

I now remember why I didn't want to use pagefield for authors. The problem concerns authors which are not members (by contrast, all members are authors).

So in member template:

For each member  : display his publi (this is fine and now works correctly).

In publi template:

For each publi, link to his author like this : Member 1, Author1, Member2 and Author2.

There is 2 cases:

  1. Import author who are member in the page field as mentioned. Use a simple textarea to list all authors and output directly. I will lost the capability to link to member's pages.
  2. Create a new page for each author within importing. It works fine but even if I unpublished the pages, they still have an url and got a link like if they are members.

How I can make the difference between authors and members? I know how to find unpublished pages with a selector but really don't know how to write the loop. When displayed on the page, they have to be in the specific order which I imported them, with a comma between names and without for the last one.

Thanks again.

Link to comment
Share on other sites

Two possible solutions for your author/member pages:

1. Use separate templates for your author and member pages. For the author Page field (used in the publi template) your "Custom selector to find selectable pages" is:

template=author|member

To differentiate between author and member in your foreach loop:

if($item->template->name == 'member') { 
    // do something only for members
}

2. Only use a single template for all authors (including members), but add a checkbox field 'member' to the author template to signal if the author is a member or not.

To differentiate between author and member in your foreach loop:

if($item->member == 1) { 
    // do something only for members
}

 

  • Like 1
Link to comment
Share on other sites

With the second option (using a checkbox) the handling would be easier in the case of non-member authors who later on may advance to member status.

What about the output of the comma separated list of authors: The loop could be something like

foreach ($authors as $a){
    $out .= $a->lastname . " " . $a->firstname . ", ";
}

Then to remove the last two characters (", ") you just use a PHP string function:

$out = substr($out, 0, -2);

 

Link to comment
Share on other sites

Hi,

Thanks again for your answers.

Since it make more sense to me, I will create authors and checking if they are members. It's works fine.

My very last question. I have now a problem with url, which should link to member's page and not author's page (that will not exist)

	foreach($page->author as $a) { 
		if($pages->count("template=member, title={$a->title}")){
				$m = $page->find("template=member, title={$a->title}"); //why this doesn't work?
				$out .= "<a href='{$m->url}'> {$a->title}</a>, "; 
			}
			else {		
				$out .= "{$a->title}, ";
			}
		}	
$out = substr($out, 0, -2);

 

@ottogal : wonderful suggestion, it did it so easily!

 

Link to comment
Share on other sites

23 hours ago, mel47 said:

Since it make more sense to me, I will create authors and checking if they are members.

In that case your template should be "author" with a checkbox "is_member".

foreach($page->author as $a) {
    if($a->is_member) {
        $out .= "<a href='{$a->url}'>{$a->title}</a>, ";
    } else {
        $out .= "{$a->title}, ";
    }
}
$out = substr($out, 0, -2);

If you don't want non-member author pages to be viewable on the front-end then at the top of the author template:

if(!$page->is_member) {
    throw new \ProcessWire\Wire404Exception();
}

 

Link to comment
Share on other sites

Finally get it (forgot a loop)!

I wanted to avoid an extra checkbox. So :

	foreach($page->author as $a) { 
		if($pages->count("template=member, title={$a->title}")){
				$member = $pages->find("template=member, title={$a->title}");
					foreach($member as $m) { 
						$out .= "<a href='{$m->url}'> {$a->title}</a>,";
				}
			}
			else		{		
				$out .= "{$a->title}, ";
			}
		}	
$out = substr($out, 0, -2);

Thanks

Link to comment
Share on other sites

  • 3 months later...

Hi,

I was thinking I solved my problem but I ran to an exception. My members are not unique, so I get some duplicates, but it shouldn't.

Now the loop is written as

  • for each author
  • if found some member
  • for each member
  • add link
  • if not, do as usual.

But I need

  • for each author
  • if they are member
  • add link
  • if not do as usual.

It sounds really strange, but I'm unable to do it even after spending hours (not kidding...). If someone could help...
Thanks!
Melanie

 

Quote

foreach($page->author as $a) { 
		if($pages->count("template=member, title={$a->title}")){
				$member = $pages->find("template=member, title={$a->title}");
					foreach($member as $m) { 
						$out .= "<a href='{$m->url}'> {$a->title}</a>,";
				}
			}
			else		{		
				$out .= "{$a->title}, ";
			}
		}	

 

 

Link to comment
Share on other sites

Why didn't you try what Robin S proposed above:

Quote
On 15.8.2016 at 2:02 AM, Robin S said:

In that case your template should be "author" with a checkbox "is_member".



foreach($page->author as $a) {
    if($a->is_member) {
        $out .= "<a href='{$a->url}'>{$a->title}</a>, ";
    } else {
        $out .= "{$a->title}, ";
    }
}
$out = substr($out, 0, -2);

 

 

  • Like 1
Link to comment
Share on other sites

Because author's page are created automatically with a CSV import. Since I didn't really want to manually check this checkbox on dozens of pages, I will prefer an other solution. I'm trying something else now, but it's not working yet, still have to think. ;-)

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

×
×
  • Create New...