Jump to content

Possible to get Field Tags?


adrian
 Share

Recommended Posts

Basically I am wanting to access all the tags (either as a string or array) for a field. Not sure if this is possible, but it would be something simple like: $field->tags

Based on what I see here: http://processwire.com/api/variables/fields/ I don't think it is possible, but maybe someone knows of another way I might easily be able to access these.

Thanks

  • Like 1
Link to comment
Share on other sites

Can you provide more information what you are trying to do? ProcessWire doesn't have anything called tags, so what you describe can be anything. Provide little bit more information/context and I am sure we can help you.

Link to comment
Share on other sites

Thanks for the quick reply.

When editing a field, under the advanced tab there is a 'Tags' field. I was hoping to be able to use this for a purpose that perhaps it is not designed for, hence the need to access this from one of my template files.

Perhaps there is a better approach to my problem. I have several fields which users need to fill out in either metric or imperial units based on their country. I was hoping to use the tags field to define the possible unit options (eg km vs miles, liters vs gallons etc). Then in my template file logic I would tell them the unit to use based on determining their country and the options for that parameter.

Hope that makes sense

Link to comment
Share on other sites

Ah, I assume you are using the DEV version?

Those tags are simply for sorting the field list in the admin. 

You will notice the templates have the same thing.

If you put something like "common" in one of them, when you go back to the fields list you will see it is now divided into "common" and "untagged"

I am not certain how to achieve what you want - but it will be possible. Just needs someone with more knowledge than me!

Link to comment
Share on other sites

Ah, those tags are only meant for combining fields to groups in admin view. So when you have lots of fields you can group them together. There is term tag in pw after all ;)

What you are describing sounds like you need to use Page field. That allows you to create dropdowns, radio buttons, checkboxes etc. and get those values in your templates. Are you familiar with Page-field yet?

Link to comment
Share on other sites

Well it turns out you can access the tags really easily: $field->data['tags'] and I think this will do exactly what I need.

I know this is not the intended usage of the tags (I realize it's just meant to help keep the admin side of things clean), but I think it might be the best option for me in this case.

I have used the Page field in a couple of cases already and it has worked a treat, but unless I am not seeing all the possibilities, I am not sure how it will work for me in this case.

For the sake of clarifying my needs, here is some code to show what I am trying to do:

   $questions = $pages->get('/modules/local-overview/questions/')->fields;

    $form = $modules->get("InputfieldForm");
    $form->action = "./";
    $form->method = "post";
    $form->attr("id+name",'subscribe-form');


    foreach($questions as $question){

        $qn = $question->name;

            $field = $modules->get("InputfieldText");

            $field->label = $question->label . ' (' . $question->data['tags'] . ')';
            $field->attr('id+name',$qn);
            $field->columnWidth = 50;
            $field->value = $answers->$qn;

            $form->append($field);
    }

This would do the trick so long as I only have one tag listed for the field. I will refine this a little better, but it is doing the trick to tell the front end user

what units to use when filling out the form. Obviously I could have the template file write out each question manually and enter the units at the end of the label, but I think this approach makes life easier. Would there be a way to use Page field in this case that I am missing?

Thanks again

Link to comment
Share on other sites

Ah, ok. You probably don't have any use for page field there.

I am not sure I understand your need yet, but sure - those tags can be used as a kind of "field metadata" outside of admin context too.

Thanks for clarification, I have better understanding now.

Link to comment
Share on other sites

Yeah, that is exactly what I need - "field metadata". I am thinking it would be a great addition to PW if it were possible to set up custom metadata fields. Really just a another field in addition to tags that accepted an array of objects would do the trick.

Anyway, the tags field is working fine for now.

Link to comment
Share on other sites

This is something I think we need to find out at one point. Every now and then people try something which doesn't work, and suddenly it works.

I have experienced this a couple times with PW, and was so far ignoring it. But I can swear, last time I did was the ColorPicker I updated, then I got something not working which I think should. Then after trying I added something in init like "$this->default;" and suddenly the default was avaiable. BUT then I removed this code and it was still working.

Is it possible some cache in DB or PW that could give sometimes a wrong result or not working but then suddenly starts working? 

Link to comment
Share on other sites

Is it possible some cache in DB or PW that could give sometimes a wrong result or not working but then suddenly starts working? 

It wouldn't be from PW's cache unless you aren't logged in, and you are using cache features. But it's not much of a mystery when you encounter that. I'm guessing it's not the case in your instance.  

Lots of the *AMP installs (like MAMP) come with a PHP cache built-in. It's certainly possible that could cause something like you've mentioned. But personally, I've not experienced any unexpected behavior like you are describing, and I'm literally using PW all day every day. :) Check and see what PHP cache you might be using, just in case. 

Link to comment
Share on other sites

Well in my case I am not sure. I am running APC, but it is quite possible that because I didn't see $field->tags in the documentation I assumed it wasn't possible. I thought I did try it, but possibly not - being in the learning phase of something new it is easy to get confused at times :)

I now know that all? the elements in the data fields are available, eg $field->columnWidth. I guess these are all missing from the docs because they are still only in the dev version?

Link to comment
Share on other sites

  • 2 months later...

Hello.

  I make a use of $field->tags quite interesting in case is helpful for somebody else.

 The problem: I want to populate a page with data of several types, images, text, etc, some of this fields have to appear in a table and I like to automatize that from the backend, having the opportunity of ordering and selecting the items I want to appear on it.

 The solution: Give the items you want to appear on the table a tag of whatever ( a choose 'table' to be original ), then in the template you use to present the data, check if the field has the tag you have choosen, then present it on the table.

Link to comment
Share on other sites

Give the items you want to appear on the table a tag of whatever ( a choose 'table' to be original ), then in the template you use to present the data, check if the field has the tag you have choosen, then present it on the table.

yeah, i've done this with lightbox galleries, give the image a 'gallery' tag and it shows in a lightbox on the page...tags rule

Link to comment
Share on other sites

ok here's something pulled straight from a place i did this:

<?php
    $gallery = $page->images->findTag('gallery');
    if(count($gallery)) {
    ?>                                
      
    <!-- start gallery -->
                                
        <?php foreach($gallery as $image) { ?>

            <!-- image markup here -->

        <?php } //end foreach ?>

    <!-- end gallery    -->  

<?php } // end if ?>
Edited by Macrura73
  • Like 2
Link to comment
Share on other sites

Here you are making ProcessWire do the same work twice (not efficient):

if(count($page->images->findTag('gallery'))>0) {
    $gallery = $page->images->findTag('gallery');

Instead, do this (efficient):

$gallery = $page->images->findTag('gallery'); 
if(count($gallery)) { // ... 
  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

Fields > advanded > tags, I gave a name of "table" to the fields I want to appear in a table on the frontend.

function renderTablaVehiculo($vehiculo){ 
    
    $page = wire('pages')->get($vehiculo);  

    $out = "<table class='table table-striped'> <tbody>";
    
    foreach($page->fields as $field) 
        // si en el backend el field tiene la etiqueta 'table' entonces aparece en la salida
        if(strstr($field->tags, "table") && $page->get($field->name)!="" ) {
            $out.= "<tr> <td> {$field->label}: </td> <td>" . $page->get($field->name) . "</td> </tr>";
        }
    
    $out.= "</tbody></table>";
    
    return $out;

}
  • Like 1
Link to comment
Share on other sites

Looks like a good creative use of tags there. I hadn't planned on them being used that way, but it seems like a good use case. It makes me think I should add a hasTag() function or something, so you wouldn't have to do something like a strstr() and would instead have reliable word boundaries between tags. 

  • Like 4
Link to comment
Share on other sites

  • 2 weeks later...

What are the odds. Totally unaware of this topic, less than two weeks later, I find myself in the need of some sort of hasTags function.
Voila http://processwire.com/talk/topic/3607-module-templatehastags/

Edit:

I now realize you might've been talking about Field Tags while the module I created was made for Template Tags. Keep that in mind.

Edited by MichaMichaMicha
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...