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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By monollonom
      (once again I was surprised to see a work of mine pop up in the newsletter, this time without even listing the module on PW modules website 😅. Thx @teppo !)
      FieldtypeQRCode
      Github: https://github.com/romaincazier/FieldtypeQRCode
      Modules directory: https://processwire.com/modules/fieldtype-qrcode/
      A simple fieldtype generating a QR Code from the public URL of the page, and more.
      Using the PHP library QR Code Generator by Kazuhiko Arase.

      Options
      In the field’s Details tab you can change between .gif or .svg formats. If you select .svg you will have the option to directly output the markup instead of a base64 image. SVG is the default.
      You can also change what is used to generate the QR code and even have several sources. The accepted sources (separated by a comma) are: httpUrl, editUrl, or the name of any text/URL/file/image field.
      If LanguageSupport is installed the compatible sources (httpUrl, text field, ...) will return as many QR codes as there are languages. Note however that when outputting on the front-end, only the languages visible to the user will be generated.
      Formatting
      Unformatted value
      When using $page->getUnformatted("qrcode_field") it returns an array with the following structure:
      [ [ "label" => string, // label used in the admin "qr" => string, // the qrcode image "source" => string, // the source, as defined in the configuration "text" => string // and the text used to generate the qrcode ], ... ] Formatted value
      The formatted value is an <img>/<svg> (or several right next to each other). There is no other markup.
      Should you need the same markup as in the admin you could use:
      $field = $fields->get("qrcode_field"); $field->type->markupValue($page, $field, $page->getUnformatted("qrcode_field")); But it’s a bit cumbersome, plus you need to import the FieldtypeQRCode's css/js. Best is to make your own markup using the unformatted value.
      Static QR code generator
      You can call FieldtypeQRCode::generateQRCode to generate any QR code you want. Its arguments are:
      string $text bool $svg Generate the QR code as svg instead of gif ? (default=true) bool $markup If svg, output its markup instead of a base64 ? (default=false) Hooks
      Please have a look at the source code for more details about the hookable functions.
      Examples
      $wire->addHookAfter("FieldtypeQRCode::getQRText", function($event) { $page = $event->arguments("page"); $event->return = $page->title; // or could be: $event->return = "Your custom text"; }) $wire->addHookAfter("FieldtypeQRCode::generateQRCodes", function($event) { $qrcodes = $event->return; // keep everything except the QR codes generated from editUrl foreach($qrcodes as $key => &$qrcode) { if($qrcode["source"] === "editUrl") { unset($qrcodes[$key]); } } unset($qrcode); $event->return = $qrcodes; })
    • By Sebi
      AppApiFile adds the /file endpoint to the AppApi routes definition. Makes it possible to query files via the api. 
      This module relies on the base module AppApi, which must be installed before AppApiFile can do its work.
      Features
      You can access all files that are uploaded at any ProcessWire page. Call api/file/route/in/pagetree?file=test.jpg to access a page via its route in the page tree. Alternatively you can call api/file/4242?file=test.jpg (e.g.,) to access a page by its id. The module will make sure that the page is accessible by the active user.
      The GET-param "file" defines the basename of the file which you want to get.
      The following GET-params (optional) can be used to manipulate an image:
      width height maxwidth maxheight cropX cropY Use GET-Param format=base64 to receive the file in base64 format.
    • By MarkE
      This fieldtype and inputfield bundle was built for storing measurement values within a field, rendering them in a variety of formats and converting them to other units or otherwise modifying them via the API.
      The API consists of a number of predefined functions, some of which include...
      render() for rendering the measurement object, valueAs() for converting the value to another unit value, convertTo() for converting the whole measurement object to different units, and add() and subtract() for for modifying the stored value by the value (converted as required) in another measurement. In the admin the inputfield includes a checkbox (which can be optionally disabled) for converting values on page save. For an example if a value was typed in as centimeters, the unit was changed to metres, and the page saved with this checkbox selected, said value would be automatically converted so that e.g. 170 cm becomes 1.7 m.

      A simple length field using Fieldtype Measurement and Inputfield Measurement.
      Combination units (e.g. feet and inches) are also supported.
      Please note that this module is 'proof of concept' at the moment - there are limited units available and quite a lot of code tidying to do. More units will be added shortly.
      See the GitHub at https://github.com/MetaTunes/FieldtypeMeasurement for full details and updates.
    • By tcnet
      File Manager for ProcessWire is a module to manager files and folders from the CMS backend. It supports creating, deleting, renaming, packing, unpacking, uploading, downloading and editing of files and folders. The integrated code editor ACE supports highlighting of all common programming languages.
      https://github.com/techcnet/ProcessFileManager

      Warning
      This module is probably the most powerful module. You might destroy your processwire installation if you don't exactly know what you doing. Be careful and use it at your own risk!
      ACE code editor
      This module uses ACE code editor available from: https://github.com/ajaxorg/ace

      Dragscroll
      This module uses the JavaScript dragscroll available from: http://github.com/asvd/dragscroll. Dragscroll adds the ability to drag the table horizontally with the mouse pointer.
      PHP File Manager
      This module uses a modified version of PHP File Manager available from: https://github.com/alexantr/filemanager
       
    • By tcnet
      This module implements the website live chat service from tawk.to. Actually the module doesn't have to do much. It just need to inserted a few lines of JavaScript just before the closing body tag </body> on each side. However, the module offers additional options to display the widget only on certain pages.
      Create an account
      Visit https://www.tawk.to and create an account. It's free! At some point you will reach a page where you can copy the required JavaScript-code.

      Open the module settings and paste the JavaScript-code into the field as shown below. Click "Submit" and that's all.

      Open the module settings
      The settings for this module are located int the menu Modules=>Configure=>LiveChatTawkTo.

       
×
×
  • Create New...