Jump to content
alexcapes

Dynamic template icon based on field value

Recommended Posts

Hi,

On a project I'm working on I have a select options field that you can select what type of content you're posting.

It has the following options:

1=Article
2=Q and A
3=Visual snippet
4=Gallery
5=List

The assigned template has an icon (file-text-o), which is great for articles but maybe not so for galleries.

Is it at all feasible/possible to change the assigned icon based on a field value within a page? Or even append a page title in the tree with an icon based on a field value?

Share this post


Link to post
Share on other sites

One way can be to not set an default icon to the template(s) but set it manually like here with other things: https://processwire.com/talk/topic/5609-display-a-template-icon-base-on-date-field/

I use this in a project where I need to show the workflow states and the current authors shortcut. There are used 3 different icons and 3 different colors.

post-1041-0-64840500-1434714605_thumb.pn

    public function addHookAfter_ProcessPageListRender_getPageLabel($event) {

        $page = $event->arguments('page');

        if ('aki-faq' == $page->template) {
            $iconTpl = $iconTpl1 = '<i class="icon fa fa-fw fa-file-text [_STATUS_]"> </i>';
            $iconTpl2 = '<i class="icon fa fa-fw fa-check-circle-o [_STATUS_]"> </i>';
            $iconTpl3 = '<i class="icon fa fa-fw fa-newspaper-o [_STATUS_]"> </i>';
            if ($page->editStatus > 2) $iconTpl = $iconTpl2;
            if ($page->editStatus > 5) $iconTpl = $iconTpl3;
            $icon = str_replace('[_STATUS_]', 'editStatus' . $page->editStatus, $iconTpl);

            $kuerzel = str_pad($page->aki_bearbeiter->kuerzel, 3, ' ', STR_PAD_RIGHT);
            $bearbeiter = (0 == $page->aki_bearbeiter->id || false === $page->aki_bearbeiter) ? ' <span class="akiPageListItem kuerzel warning">---</span> ' : ' <span class="akiPageListItem kuerzel">' . $kuerzel . '</span> ';

            $event->return = $icon . $bearbeiter . $event->return;
        }
    }
Edited by horst
  • Like 7

Share this post


Link to post
Share on other sites

Thank you horst, this is exactly what I was looking for.

For those interested, this is my working module below, which adds in icons based on an select options field.

<?php
class DynamicIcons extends WireData implements Module {

public static function getModuleInfo() {
return array(

'title' => 'Dynamic icons for the page tree based on field values',
'version' => 1,
'summary' => 'Module to change the icon on the page tree depending on field values.',
'singular' => true,
'autoload' => true,
);
}

public function init() {
        $this->addHookAfter('ProcessPageListRender::getPageLabel', $this, 'addPageListLabelItems');
    }

    public function addPageListLabelItems($event) {
        $page = $event->arguments('page');

$page->of(false);

// Check options field for correct value
if($page->content_type == '1') {

// Add fontawesomeicon
$styleItem = "<i class=\"fa fa-file-text-o\"></i>";

// Add to pagetree
$event->return = $styleItem . $event->return;
}
    }
}

(This just shows one options but obviously you could add in additional icons for different field values)

  • Like 9

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By quickjeff
      Hi Guys, 
      I have been debugging a site for the last 2 hours and cannot solve the issue. 
      I have a site running on 3.0.148. 
      I installed the Kongondo Blog module and was updating the templates to include the website style. 
      Once everything was set and done, I checked the page tree to see an error appear. 
      Template must be assigned a name before 'filename' can be accessed
      The same error appears in templates. 
      Debugging Steps
      I checked the templates in the server to ensure I didnt accidentally delete the namespace.  Deleted cache in browser and server under assets Still no go. 
      Any help is appreciated. 
      Thanks! 
    • By Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By DV-JF
      Hey all, hope you're feeling well these days!
      Short question: Under "Setup > Templates" it's possible to sort the templates by last modified. Is this possible for fields view to?
      Long explanation:
      Currently I am in the process of programming a reusable template and often have to copy several fields & templates from different PW installations into my "master version".
      Therefore it would be good to see already in the field list when the last modification has taken place.
      Stay healthy & don't forget to wash you hands - many greets!
    • By Guy Incognito
      This short script loops through some images from an XML feed and pushes new ones to an image field. It all works perfectly, except for some reason the last image (only) in the loop each time doesn't receive the image description... can everyone spot why? TIA! 🙂 
      foreach ($propertyImages as $img) { $fileName = trim($img[0]); if ( !empty($fileName) ) { $imgPath = '../property_data/'.$fileName; if(file_exists($imgPath) && !in_array(strtolower($fileName),$currentImages)) { $p->property_images->add($imgPath); $p->save(); $newImg = $p->property_images->last(); $newImg->description = $img[1]; $p->save(); } } }  
    • By MateThemes
      Hello everyone!
      I am working with Processwire since some time. But some topics are quite hard for me.
      I have a Portfolio (Gallery) Page.
      I am build a template with Portfolio Index and pages with portfolio entries.
      Structure:
      Portfolio Index
      -- Portfolio Entry
      -- Portfolio Entry
      and so on.
      Portfolio Entry has an Image field with max 12 images and are accessible Templates. 
      Now I want to display the single Portfolio Entry on the Portfolio Index and Paginate them. In the index page all images of a single Entry page should be displayed (I should not be organized as albums, where a random image of the portfolio entry should be displayed). 
      I have no clue to achieve this. May someone could give me an advice.
      Thank you in advance!
×
×
  • Create New...