Jump to content

FieldtypePageWithDate & InputfieldPageWithDate


Raymond Geerts
 Share

Recommended Posts

FieldtypePageWithDate & InputfieldPageWithDate

Module for ProcessWire - Page Reference with Date Field - Field that stores one or more references to ProcessWire pages

Modified version of the FieldtypePage module in ProcessWire with extra datetime field containing the date a page was added to the inputfield.

Github URL

Link: FieldtypePageWithDate

To install

Copy to /site/modules/ and go to Admin > Modules > Check for new modules.

Requirement

Requires: InputfieldPageWithDate (will be installed automaticly)

Tested on ProcessWire 2.5.6 dev

Setup in back-end

  • install both modules:
    • FieldtypePageWithDate
    • InputfieldPageWithDate
  • create a new field in ProcessWire and give it a name
    • eg. myfriends
  • as type choose PageWithDate
  • choose the right dereference for your needs (all 3 modes work fine with this field)
    • Multiple pages (PageArray) (in this example we are using this)
    • Single page (Page) or boolean false when none selected
    • Single page (Page) or empty page (NullPage) when none selected
  • assign a selector to the field, in this example we will use users
    • Template of selectable page(s) (select the user template)
  • choose a label field, since we are using users set it to name
    • Label Field name
  • set an input field type, depending if you are using dereference of multiple or a single page
    • for this example we are using AsmSelect
  • save the field and assign it to a template of choice
  • Add some users/friends to the field by editing a page with this template

Usage in front-end

In you template you can acces the field as you usualy do with any FieldtypePage.

In addition to this the new parameter "assigned" is available

Multiple pages

if (count($page->myfriends)) {
    foreach($page->myfriends as $friend) {
        echo "id: ".$friend->id."<br>";
        echo "name: ".$friend->name."<br>";
        echo "assigned on: ".$friend->assigned."<br><br>";
    }
}

This will output something like:

id: 1031
name: johndoe
assigned on: 2014-10-31 14:22:02

id: 1032
name: janedoe
assigned on: 2013-04-15 23:16:38

Note: To use your own data/time formatting set $friend->of(false); to get a unix timestamp from the database

Single page

echo "id: ".$page->myfriends->id."<br>";
echo "name: ".$page->myfriends->name."<br>";
echo "assigned on: ".$page->myfriends->assigned."<br>";
This will output something like:

id: 1031
name: johndoe
assigned on: 2014-10-31 14:22:02

Note: To use your own data/time formatting set $page->myfriends->of(false); to get a unix timestamp from the database

Edited: removed extra date() formatting since value is formatted by default.

Set assigned datetime manually

You can set datetime manually by assigning it to the page you add

$friend = $users->get('johndoe');
$friend->assigned = "2012-01-01 12:12:12";
$page->myfriends->add($friend);
$page->of(false);
$page->save();

Edited: Added information on manually setting the assigned to a datetime value. (if you previously installed this module you need to update atleast the FieldtypePageWithDate.module file)


Edited by Raymond Geerts
  • Like 11
Link to comment
Share on other sites

@Raymond

I'm trying this out now and seem to have hit a problem. I can see that the assigned column is getting stored correctly in the DB but when I access the assigned time as per your example in the opening post I always get "1970-01-01 01:33:34" - which is obviously incorrect.

Edited to add: I think your example code should be...

if (count($page->myfriends)) {
    foreach($page->myfriends as $friend) {
        echo "id: ".$friend->id."<br>";
        echo "name: ".$friend->name."<br>";
        echo "assigned on: ".$friend->assigned."<br><br>";
    }
}

...as the assigned field is already formatted. Double-formatting it causes the problem (at least it does in my case.)

Edited by netcarver
  • Like 1
Link to comment
Share on other sites

Thanks for sharing this module. I'm thinking of using it in a project but need to know if the assigned value is API settable too - I'm presuming it is but want to be sure.

This is something i havent tried yet, so at the moment i think its not possible. I will take a look at it and if it can be build in i will add it and update the module(s)

I'm trying this out now and seem to have hit a problem. I can see that the assigned column is getting stored correctly in the DB but when I access the assigned time as per your example in the opening post I always get "1970-01-01 01:33:34" - which is obviously incorrect.

You are right about that, somehow i messed it up, looking at my code i tested with i see i turned output filtering off so thats must be where it went wrong :)

I have edited the instructions here and on github. Thanks for noticing

Link to comment
Share on other sites

Thanks for sharing this module. I'm thinking of using it in a project but need to know if the assigned value is API settable too - I'm presuming it is but want to be sure.

I have added the ability to set the assigned datetime manually. This can be done by setting the assigned value to the page you going to add.

For example:

$friend = $users->get('johndoe');
$friend->assigned = "2012-01-01 12:12:12";
$page->myfriends->add($friend);
$page->of(false);
$page->save();

When you want to modify the value of an already assigned page you will have to remove it first and then add it again with a new assigned datetime value.

$friend = $users->get('johndoe');
$page->myfriends->remove($friend);
$page->of(false);
$page->save();

$friend->assigned = "2012-01-01 12:12:12";
$page->myfriends->add($friend);
$page->of(false);
$page->save();
  • Like 5
Link to comment
Share on other sites

  • 2 months later...

I'm having an issue using this module.
my code is this:

if($input->post->addfriend && $user->isLoggedin()){
  $page->of(false);
  $page->friends_waiting->add($user);
  $page->save();
}

I get this error

Warning: array_filter() expects parameter 1 to be array, null given in /home/public_html/site/modules/FieldtypePageWithDate/FieldtypePageWithDate.module on line 280
Link to comment
Share on other sites

I dont see where you set $user->assigned in your code. You could try the following, hoping that it will solve your problem. (note the move of $page->of(false); )

if ($input->post->addfriend && $user->isLoggedin()) {
    $user->assigned = date("Y-m-d H:i:s");
    $page->friends_waiting->add($user);
    $page->of(false);
    $page->save();
}
  • Like 1
Link to comment
Share on other sites

I'm sorry but the problem still persist. Strange thing is after the error has been displayed the code is executed and it works...just got that error and since I'm using ajax call to perform that code I stuck there because of the error.

Edit: disabling debug mode on PW I get this working...

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...

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.
×
×
  • Create New...