Jump to content
mel47

Add to pagefield after creation

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

Share this post


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

 

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Try this:

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

Share this post


Link to post
Share on other sites

Or:

// get publication from this author
$publi = $pages->find("template=publication, author=$page");

Author and Member are the same thing, right?

Share this post


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

Share this post


Link to post
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");

 

Share this post


Link to post
Share on other sites
$author = $pages->find("template=member, title=$page");

This part doesn't make sense unless you have a page using the member template whose title is a page ID. I don't think you need this line.

Share this post


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

Share this post


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

Share this post


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

 

Share this post


Link to post
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!

 

Share this post


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

 

Share this post


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

Share this post


Link to post
Share on other sites

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}, ";
			}
		}	

 

 

Share this post


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

Share this post


Link to post
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. ;-)

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 DV-JF
      Hey all, hope you're feeling well these days!
      Short question: Under "Setup > Templates" it's possible to sort the templates by last modified. Is this possible for fields view to?
      Long explanation:
      Currently I am in the process of programming a reusable template and often have to copy several fields & templates from different PW installations into my "master version".
      Therefore it would be good to see already in the field list when the last modification has taken place.
      Stay healthy & don't forget to wash you hands - many greets!
    • By MateThemes
      Hello everyone!
      I have a markup and image field question:
      I have a markup in which a gallery preview should be displayed on home page. This gallery have a special markup, 3 of 4 images have special image ratio.

      The gallery looks like this. 
      The html markup is like this:
      <div class="uk-section-muted" uk-scrollspy="target: [uk-scrollspy-class]; cls: uk-animation-slide-top-small; delay: 200;"> <div data-src="./assets/images/arrahof/home-restaurant-bg.svg" class="uk-background-norepeat uk-background-contain uk-background-top-center uk-section uk-section-large" uk-img> <div class="uk-container uk-container-small"> <div class="uk-margin-large" uk-grid> <div class="uk-width-1-1@m"> <h2 class="uk-text-center" uk-scrollspy-class>Das Angebot im ArraHof</h2> </div> </div> <div class="uk-grid-small uk-grid-margin-small" uk-grid> <div class="uk-width-expand@s"> <div class="uk-margin-remove-vertical uk-text-center" uk-scrollspy-class> <a class="el-container uk-inline-clip uk-transition-toggle uk-link-reset" href="#"> <img class="el-image" data-src="https://via.placeholder.com/610x604" data-sizes="(min-width: 610px) 610px" data-width="610" data-height="604" alt="Placeholder Image" uk-img> <div class="uk-overlay-default uk-transition-fade uk-position-cover"></div> <div class="uk-position-center uk-position-small"> <div class="uk-overlay uk-padding-large uk-transition-fade uk-margin-remove-first-child"> <h3 class="el-title uk-h4 uk-heading-divider uk-transition-slide-top-small uk-margin-top uk-margin-remove-bottom">Lorem Ipsum</h3> <div class="el-content uk-panel uk-transition-slide-bottom-small uk-margin-top">Comfort Food</div> </div> </div> </a> </div> </div> <div class="uk-width-expand@s"> <div class="uk-margin-remove-vertical uk-text-center" uk-scrollspy-class> <a class="el-container uk-inline-clip uk-transition-toggle uk-link-reset" href="#"> <img class="el-image" data-src="https://via.placeholder.com/610x604" data-sizes="(min-width: 610px) 610px" data-width="610" data-height="604" alt="Placeholder Image" uk-img> <div class="uk-overlay-default uk-transition-fade uk-position-cover"></div> <div class="uk-position-center uk-position-small"> <div class="uk-overlay uk-transition-fade uk-margin-remove-first-child"> <h3 class="el-title uk-h4 uk-heading-divider uk-transition-slide-top-small uk-margin-top uk-margin-remove-bottom">The Williams family</h3> <div class="el-content uk-panel uk-transition-slide-bottom-small uk-margin-top">Comfort Food</div> </div> </div> </a> </div> </div> </div> <div class="uk-grid-small uk-grid-margin-small" uk-grid uk-height-match="target: .uk-card; row: false"> <div class="uk-width-2-3@s"> <div class="uk-margin-remove-vertical uk-text-center" uk-scrollspy-class> <a class="el-container uk-inline-clip uk-transition-toggle uk-link-reset" href="#"> <img class="el-image" data-src="https://via.placeholder.com/610x400" data-width="610" data-height="400" alt="Placeholder Image" uk-img> <div class="uk-overlay-default uk-transition-fade uk-position-cover"></div> <div class="uk-position-center uk-position-small"> <div class="uk-overlay uk-transition-fade uk-margin-remove-first-child"> <h3 class="el-title uk-h4 uk-heading-divider uk-transition-slide-top-small uk-margin-top uk-margin-remove-bottom">The Williams family</h3> <div class="el-content uk-panel uk-transition-slide-bottom-small uk-margin-top">Comfort Food</div> </div> </div> </div> </a> </div> <div class="uk-width-expand@s"> <div class="uk-margin-remove-vertical uk-text-center" uk-scrollspy-class> <a class="el-container uk-inline-clip uk-transition-toggle uk-link-reset" href="#"> <img class="el-image" data-src="https://via.placeholder.com/610x820" data-sizes="(min-width: 610px) 610px" data-width="610" data-height="820" alt="Placeholder Image" uk-img> <div class="uk-overlay-default uk-transition-fade uk-position-cover"></div> <div class="uk-position-center uk-position-small"> <div class="uk-overlay uk-transition-fade uk-margin-remove-first-child"> <h3 class="el-title uk-h4 uk-heading-divider uk-transition-slide-top-small uk-margin-top uk-margin-remove-bottom">The Williams family</h3> <div class="el-content uk-panel uk-transition-slide-bottom-small uk-margin-top">Comfort Food</div> </div> </div> </div> </a> </div> </div> </div> <div class="uk-margin-large" uk-grid> <div class="uk-width-1-1@m"> <div class="uk-text-lead uk-width-xxlarge uk-margin-auto uk-text-center" uk-scrollspy-class>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur.</div> <div class="uk-margin-medium uk-text-center" uk-scrollspy-class> <a class="el-content uk-button uk-button-default uk-button-large" href="#">Unterkunft</a> </div> </div> </div> </div> </div> How can I achieve when I have an image field to add the custom markup within the image field? I have no clue to get this done.
      Thank you!
    • By Robin S
      This module is inspired by and similar to the Template Stubs module. The author of that module has not been active in the PW community for several years now and parts of the code for that module didn't make sense to me, so I decided to create my own module. Auto Template Stubs has only been tested with PhpStorm because that is the IDE that I use.
      Auto Template Stubs
      Automatically creates stub files for templates when fields or fieldgroups are saved.
      Stub files are useful if you are using an IDE (e.g. PhpStorm) that provides code assistance - the stub files let the IDE know what fields exist in each template and what data type each field returns. Depending on your IDE's features you get benefits such as code completion for field names as you type, type inference, inspection, documentation, etc.
      Installation
      Install the Auto Template Stubs module.
      Configuration
      You can change the class name prefix setting in the module config if you like. It's good to use a class name prefix because it reduces the chance that the class name will clash with an existing class name.
      The directory path used to store the stub files is configurable.
      There is a checkbox to manually trigger the regeneration of all stub files if needed.
      Usage
      Add a line near the top of each of your template files to tell your IDE what stub class name to associate with the $page variable within the template file. For example, with the default class name prefix you would add the following line at the top of the home.php template file:
      /** @var tpl_home $page */ Now enjoy code completion, etc, in your IDE.

      Adding data types for non-core Fieldtype modules
      The module includes the data types returned by all the core Fieldtype modules. If you want to add data types returned by one or more non-core Fieldtype modules then you can hook the AutoTemplateStubs::getReturnTypes() method. For example, in /site/ready.php:
      // Add data types for some non-core Fieldtype modules $wire->addHookAfter('AutoTemplateStubs::getReturnTypes', function(HookEvent $event) { $extra_types = [ 'FieldtypeDecimal' => 'string', 'FieldtypeLeafletMapMarker' => 'LeafletMapMarker', 'FieldtypeRepeaterMatrix' => 'RepeaterMatrixPageArray', 'FieldtypeTable' => 'TableRows', ]; $event->return = $event->return + $extra_types; }); Credits
      Inspired by and much credit to the Template Stubs module by mindplay.dk.
       
      https://github.com/Toutouwai/AutoTemplateStubs
      https://modules.processwire.com/modules/auto-template-stubs/
    • By DV-JF
      Hi, 
      I'm using this kind of setup (https://processwire.com/blog/posts/language-access-control-and-more-special-permissions/#language-page-edit-permissions) in order to control the page edit permissions. Now I'm wondering if it's possible to hide the "none-ediable" language-tabs instead of striking them through.

      Many greets...
       
×
×
  • Create New...