Jump to content

Field Name Unprefixer


owzim
 Share

Recommended Posts

GitHub: https://github.com/owzim/FieldnameUnprefixer
 
From the README.md:
 
Makes it possible to access prefixed field names without the prefix.
 
Usage
 
Create some prefixed fields and add it to you desired template, for example prefix_myField and otherPrefix_myOtherField.
Configure the prefixes, that should be removed in your /site/config.php, like so:

$config->FieldnameUnprefixer = array(
    'basic-page' => array('prefix_', 'otherPrefix_'),
    'home' => array('prefix_', 'otherPrefix_')
);

The first array keys are the template names.
The Prefixes also can be assigned as a string if it's just a single one:

$config->FieldnameUnprefixer = array(
    'basic-page' => 'prefix_',
    'home' => 'otherPrefix_'
);

Now you can access the fields without the prefix, like so:

echo $page->myField;
echo $page->myOtherField;

The unprefixed field names are now also accessible via selectors, but only for already fetched data, since the field is not really in the database:

// not
$pages->find('myField=something');
// but
$pages->find('template.name=basic-page')->filter('myField=something');

Use case
 
Prefixes are sometimes necessary to have fields that behave specific to the template, so you need to create a specific field and namespace it with a prefix, but don't want to access the fields differently on each template.

// eeew!
$client->client_address;
$something->something_address;

// yay!
$client->address;
$something->address;

Might be not the ProcessWire-way because data access is abstracted, but I use it, and perhaps some of you find it useful too.

  • Like 9
Link to comment
Share on other sites

  • 3 months later...

Hi Mike,

not possible at the moment, but I just looked into the code, should be easy to implement, with selectors.

Instead of $templates->get("name"), I would use $templates->find("selector"), and loop through them.

But it needs to be tested as well and unfortunately I haven't got the time at the moment.

Feel free to make a PR :)

PS: it's owzim ;)

  • Like 1
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...