Jump to content

Getting all fields in a list or array from a page.


Harmster
 Share

Recommended Posts

Hey,

I am quite new to Processwire and to this forum ,as you can see. Although I am really starting to become a fan of processwire. But the amount of topics on the forums is somewhat limited. Doesn't matter but I can't really find the answer to my question.

I do want to retrieve all the fields (type's and values) from a page, and display them. I do got this:

<?php
$u = new nubUsers();
if($u->is_manager()){
$id = $input->get('id');
$p = new NubPages();
$r = $p->edit_page($id);
$f = $fields->find("page=$r->id");//get all the fields
?>
<h1>Edit page <?php echo $r->title; ?></h1>
<?php
}else{
throw new Wire404Exception;
}
?>

With edit_page would be:

public function edit_page($id){
$p = wire('pages')->get("id=$id");
return $p;

}

How could I get this done?

Sorry if this is already answered, explained somewhere else or anything like that, but if so i couldn't really find it :\

Kind regards,

Harmster.

  • Like 1
Link to comment
Share on other sites

I'm not sure I understand the example. What is NubUsers and NubPages? Also something like $fields->find("page=$r->id"); is completely unfamiliar to me. It seems like there are some parts ProcessWire and some parts something else? :)

I do want to retrieve all the fields (type's and values) from a page, and display them.

Here's a simple example that does exactly that:

foreach($page->fields as $field) {
 echo "<p>"; 
 echo "Field: {$field->name}<br />";
 echo "Type: {$field->type}<br />";
 echo "Value: " . $page->get($field->name); 
 echo "</p>"; 
}
  • Like 5
Link to comment
Share on other sites

I'm not sure I understand the example. What is NubUsers and NubPages? Also something like $fields->find("page=$r->id"); is completely unfamiliar to me. It seems like there are some parts ProcessWire and some parts something else? :)

Here's a simple example that does exactly that:

foreach($page->fields as $field) {
echo "<p>";
echo "Field: {$field->name}<br />";
echo "Type: {$field->type}<br />";
echo "Value: " . $page->get($field->name);
echo "</p>";
}

Thank you for your quick reply!

yeah, i kind of made my own classes to get a bunch of repatative stuff done.

I feel stupid that it is that simple...

Ty Ryan!

Link to comment
Share on other sites

Harmster, have you already seen the cheatsheet? It's really helpful for figuring out stuff like this. For example $page->fields is in there. Clicking on the items give a small description of what it is.

Also, if your doing some 'custom' user stuff it might be worth noting that you can edit the user template just like other templates. You can add your own fields. A user is a page object, see Admin->Access->Users.

  • Like 1
Link to comment
Share on other sites

Harmster, have you already seen the cheatsheet? It's really helpful for figuring out stuff like this. For example $page->fields is in there. Clicking on the items give a small description of what it is.

Also, if your doing some 'custom' user stuff it might be worth noting that you can edit the user template just like other templates. You can add your own fields. A user is a page object, see Admin->Access->Users.

Thank you for your response, I forgot about the cheatsheet i guess... still new to all this...

Although I did edit the template and I did make some custom classes to do some repetative actions on that template etc.

Link to comment
Share on other sites

  • 8 months later...

Sorry. And In case I need to get just one field from a specific page?
I'd want to get the value of the field "enlace" (where I keep an URL value) in the page "store" to use in the main menu in all the pages. How would you do it?

Do I need to do a foreach anyway?

Link to comment
Share on other sites

Sorry. And In case I need to get just one field from a specific page?

I'd want to get the value of the field "enlace" (where I keep the an URL value) in the page "store" to use in the main menu in all the pages. How would you do it?

Do I need to do a foreach anyway?

Rule of thumb:

many things -> foreach

one thing -> grab and echo directly

In your case, if what you want is on another page

echo $pages->get(123)->enlace; //123 is ID of that page

You can also use path to that page instead of ID. Please have a look at the API documentation. Everything to get your started is explained there http://processwire.com/api/. Look at variables and selectors in the first instance.

Edit: Quick addition for newbies :) - what Harmster wanted in the first post were the names and the values of Fields which is different from what gfdesign needs

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

You can also add the ID to the "list of fields to display in the admin page list" field on the advanced tab on each template.

To do it quickly for all templates, put this code on any page and load it:

// get all templates
foreach ($templates as $t) {

    // if fields are the default (empty shows only the title)
    if ($t->pageLabelField == "") {

        // make new string with title and id
        $t->pageLabelField = "title id";

    } else {

        // if not, add " id" to the existing string
        $t->pageLabelField .= " id";

    }
    // save the template
    $t->save();
}
 
  • Like 1
Link to comment
Share on other sites

And to revert (removes the id from the end of the string):

foreach ($templates as $t) {
    $t->pageLabelField = preg_replace('/ id$/', '', $t->pageLabelField);
    $t->save();
}
Link to comment
Share on other sites

  • 2 years later...
I am creating a custom search routine and this code has been very useful to encompass all fields of the site pages.



foreach($page->fields as $field) {
echo "<p>";
echo "Field: {$field->name}<br />";
echo "Type: {$field->type}<br />";
echo "Value: " . $page->get($field->name);
echo "</p>";
}


But I failed to get the subfields of repeater's field type 

Any suggestions?

Link to comment
Share on other sites

  • 1 month later...

@tekno,

can you explain more about what you want to do? there are several ways to get a list of unique values, but depends on how the fields are setup and how your search form needs to reference them.

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