Jump to content

Field Generator


Pierre-Luc
 Share

Recommended Posts

Field Generator is a module to help you create random character strings and automatically assign them to one or multiple fields of your choice when a page is created.

When setup, it will automatically do so whether you create a page through the administration panel or through code, meaning you don’t need to set $page->name or any other field of your choice, it just works automagically.

Field Generator uses PHP’s openssl_random_pseudo_bytes() function to generate its random strings. It thus has both PHP >= 5.3.0 and OpenSSL as dependencies.

Usage

After installation, add rules through Setup > Field Generator. That’s it! Rules only run when first creating the page.

For more information : https://github.com/plauclair/FieldGenerator


Release : Version 0.9.0

This is the initial release. This is near stable and you shouldn’t be too worried to use it in production. Here is a screencast on how to use it.

  • Like 10
Link to comment
Share on other sites

  • 2 weeks later...

Version 0.9.9 - Generators for all! Download on GitHub

0.9.9 fixes an access problem preventing users other than the superadmin to use it and other minor bugs.

I've removed the ability to delete the stored settings, I figured if someone disabled the module temporarily and wanted to reactivate it, you shouldn't lose your settings for that. What do you guys think?

Link to comment
Share on other sites

  • 3 months later...

Hi guys

I'm using the Field Generator module to generate random strings of numbers and assign them to one field of my choice when a page is created. 

In the module description, it says we don't need to set $page->name (or any other field), it just works automagically. 

As in my case, I'm using the module to create urls (for example, mysite.com/image/131602236844), so I'd need to use the $page->name field to create the string, but it won't work. It works on any other custom field, but not with "name" field.

Here are a few screenshots:

Field Generator setup with "name" field: http://api.drp.io/files/544295d764173.png

But nothing happens on the "name" field: http://api.drp.io/files/544295d75bb0e.png // http://api.drp.io/files/544295d7523ad.png

I've already tried putting "name" and "$page->name" but nothing happens. 

Any ideas if I'm doing something wrong?

Edited by kongondo
Moved your topic to the module's support forum
Link to comment
Share on other sites

I don't know how the module works. But 'name' is a reserved word for the pagename, which is stored in database table 'pages'. It is not a field like 'title', which is assigned to the page. Try another word than 'name'.

some other reserved words, which cannot be used for field names:

  • created
  • modified
  • status
  • sort
Link to comment
Share on other sites

Maybe PM Pierre-Luc the module author or file an issue in the project's page at GitHub since the module does not seem to have a support forum....

I've tried :(

I don't know how the module works. But 'name' is a reserved word for the pagename, which is stored in database table 'pages'. It is not a field like 'title', which is assigned to the page. Try another word than 'name'.

some other reserved words, which cannot be used for field names:

  • created
  • modified
  • status
  • sort

Hmm, I've figured this... so there's no way to do custom urls?

Link to comment
Share on other sites

is this not the support forum?

https://processwire.com/talk/topic/6727-field-generator/

in the video it clearly shows 'name' as the field being targeted. Could be a compatibility issue if it is not working..

maybe you have to check OpenSSL dependency?

you could easily write a much simpler module (see example code below), or use processwire's new automatic page name feature (i'm using it a lot)..

here is an example of a simple module to create a randomy page name, using PHP uniqid() function (untested):

<?php

public function generateRandomName() {
        if ($this->input->get->parent_id == 1019) { // 1019 = some page where we want to auto-generate child page names
            $page = new Page();
                $page->parent = $this->input->get->parent_id;
                $page->template = 'child-template-name';

            $page->name = uniqid();
            
            $page->addStatus(Page::statusUnpublished);
            $page->save();
            
            $this->session->redirect("../edit/?id=$page");
        }
    }
  • Like 3
Link to comment
Share on other sites

processwire's new automatic page name feature (i'm using it a lot)..

$page->name = uniqid();           

@Macrura

This is not processwires new automatic page name feature. It is a php function which generates a hex converted string of microtime().

Since it is quite unlikely that the same page name is generated your example will work, but it is not a clean solution.

Edit: Forget about the preceding line :rolleyes:

If you want to generate a page by processwires api with a unique page name, you have to could use the add() method. No need to instantiate a page object in your template.

$parent = 'parentname';
$template = 'templatename';
$name = 'foo';
$pages->add($template,$parent,$name);

With this function you get pages with the title 'foo' and the name 'foo-[n]'. [n] is a counter which makes the name unique.

You could add a fourth parameter to set other page properties like status or so. The fourth parameter has to be an array.

$pages->add($template,$parent,$name,array('status'=>1));

Even if you leave out $page->name = uniqid(); in your example the page will be generated unique.

In both cases the counter doesn't work properly since it counts all siblings and not only those with the same name.

Edited by kixe
  • Like 3
Link to comment
Share on other sites

@kixe - sorry for some misunderstanding/misreading of my post (it wasn't clear); what i was trying to say is you could the built in auto-name (in the admin):

post-136-0-63263000-1415625293_thumb.png

OR make your own 'simpler' module, which is what i was trying to give an example of. Since the module in question (Field Generator) uses a function with openSSL dependency and the OP was not able to get said module to work, the use of uniqid() is there to give the possibility of a random looking page name as an alternative; but it is probable that the results would not be much different than using Ymdhis as your field setting

Link to comment
Share on other sites

or use processwire's new automatic page name feature (i'm using it a lot)..

@macrura

Ahh, thats what you are talking about

For everybody:

If you want to use this feature you have to create a new field of fieldtype PageTable.

'Automatic Page Name Format' could be set under the 'Input' Tab.

Since it is quite unlikely that the same page name is generated your example will work, but it is not a clean solution.

Edit: Forget about the preceding line :rolleyes:

I tried it out again and I have to correct myself again.

Not good, because you will get an mysql error if you do it more than one time

$page = new Page();
$page->parent = 'parentname';
$page->template = 'templatename';
$page->name = 'foo';
$page->save();

Better, because name will be autogenerated unique.

$page = new Page();
$page->parent = 'parentname';
$page->template = 'templatename';
$page->title = 'foo';
$page->save();

// or

$page = new Page();
$page->parent = 'parentname';
$page->template = 'templatename';
$page->save();
Link to comment
Share on other sites

Hey guys, sorry for not answering for a while, I've been quite busy with other things lately. 

All fields should be supported, name included. It is compatible with 2.4 and 2.5, I have a few sites running it without any problem.

As was mentioned, there are dependencies that MUST be met, check the GitHub page for documentation.

pO3Us4A.png

Link to comment
Share on other sites

Questions:

1) do you have any of the ProcessWire language modules enabled? If so, which ones?

2) Do you still have problems with the name field when you use letters in the dictionary (I had this happen once, but I thought I fixed it)

Hi Pierre,

1) nope

2) i'm using only numbers, but i tried letters to test and it doesn't work either

btw, i'm using the latest processwire version (2.5.2)

in the video it clearly shows 'name' as the field being targeted. Could be a compatibility issue if it is not working..

maybe you have to check OpenSSL dependency?

Yes, it's enabled.

Link to comment
Share on other sites

  • 2 weeks later...

@Hafa,

I suggest you first rule out if MAMP/OpenSSL is the issue. I don't use MAMP myself so I don't know how to enable OpenSSL. Maybe others will chime in but meanwhile you can find out. Can you also confirm you are running the required PHP version? Additionally, to rule out the problem of your environment, you can test your site in lightning.pw....(when it's back online :-))

Link to comment
Share on other sites

Hi @Macrura!

I wanted to test your code right way but I'm not sure where I should include it? A functions.php file?

And can the "uniqid" function generate numbers only? Is it possible to set a length?

Thank you very much for taking the time to try help me out!  ^_^

here is an example of a simple module to create a randomy page name, using PHP uniqid() function (untested):

<?php

public function generateRandomName() {
        if ($this->input->get->parent_id == 1019) { // 1019 = some page where we want to auto-generate child page names
            $page = new Page();
                $page->parent = $this->input->get->parent_id;
                $page->template = 'child-template-name';

            $page->name = uniqid();
            
            $page->addStatus(Page::statusUnpublished);
            $page->save();
            
            $this->session->redirect("../edit/?id=$page");
        }
    }

@Kongondo Waiting for lightning.pw to get back online! Thank you for the tip!  ^_^ 

Link to comment
Share on other sites

@hafa - this would need to be inside a module - maybe take a look at the helloworld module, or check this out:

https://processwire.com/talk/topic/1908-automatically-generating-new-page-names/?p=48421

as far as uniqueid - you would want to use that if you don't care about anything with security, just for random numbers;

if you wanted something more cryptographically sound, maybe try flourish fCryptography - there is a flourish autoloader module for processwire.

- - -

http://php.net/manual/en/function.uniqid.php

Warning

This function does not create random nor unpredictable strings. This function must not be used for security purposes. Use a cryptographically secure random function/generator and cryptographically secure hash functions to create unpredictable secure IDs.

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...