Jump to content

How to use fields selector?


Manaus
 Share

Recommended Posts

Hello, I don't grasp the use of a selector like

$fields->get("name")

or

$fields->find("selector")

I see in the cheatsheet, since when I need to print a custom field from the page I just need to write

$page->mycustomfield

or similar.

How can I use the $fields object?

Thanks!

Link to comment
Share on other sites

46 minutes ago, Manaus said:

How can I use the $fields object?

There's lots of use cases mainly related to module development as well as internal ProcessWire use. You can also use it (with caution) for housekeeping. Let's say you created a number of fields for testing purposes on a site or module you are working on. You gave your fields a specific prefix to easily identify them as test fields, for instance. You now want to delete them. They are not in use in any template. There are a lot of them. Instead of deleting them manually, you can use the API to do it for you.

<?php namespace ProcessWire;
// get your 'test' fields
// their names start with 'test', e.g. 'test_some_field'
$testFields = $fields->find("name^=test");
d($testFields,'TEST FIELDS');
foreach($testFields as $testField){
    $fields->delete($testField);
}

Run that in Tracy console and you are done.

Edited by kongondo
typos
  • Like 3
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.
×
×
  • Create New...