Jump to content
Soma

MarkupSimpleNavigation

Recommended Posts

Next little tutorial on this really great module...

Getting a Footermenu stripped from the mainmenu and output in a footermenu

1. Setup Pagefield for your /settings/ or /tools/ page

This solution could be set by the user direct via a simple pagefield to select normal contentpages that a normaly in the mainmenu...but this items should stay not there and render somewhere else...

post-2327-0-42569000-1442412709_thumb.jp

2. Strip the choosen pages from the mainmenu

//get the pagefield content
$footerMenu = $settings->footer_menu;

//just the needed option listed - other options like needed for your menu
$options = array(
       'selector' => 'id!='.$footerMenu.'',    // define custom PW selector
);

3. Output the pagefield items

post-2327-0-41513000-1442412792_thumb.jp

<?php //in my example i am in the _main.php template and render some footermenu
if ($settings->footer_menu) { //check for the pagefield
    echo '<div class="eight columns">';
    foreach ($settings->footer_menu as $item) {
        if (!$item->is('hidden') || $item->is('unpublished')) { //check for hidden/unpublished pages!!
	    echo '<a href="'.$item->url.'" class="button sendbtn btnSend">'.$item->title.'</a>';
	}
    }
    echo '</div>';
}
?>

hope this is helpful...have fun.

I don't have a blog and i am a fan of keeping things together....so don't bother if i'm posting such things here and write me if such usecases are not helpful or nonsense...... ;)

Best regards mr-fan

  • Like 2

Share this post


Link to post
Share on other sites

Is it possible to use the "selector" option to choose menu items based on parent?

I tried...

"selector" => "parent=1020"

...and...

"selector" => "parent=/my-page/"

...but neither of these worked.

I know I can do this by setting a PageArray as the root object but I'm trying to understand why I can't do this with the selector option.

Share this post


Link to post
Share on other sites

No it's not possible, it iterates the tree structure as is. "parent -> childrens -> childrens".

You can only set the top root items per a PageArray. Form there these are iterated down the tree. The selector only is used to filter the children of the current nodes.

Share this post


Link to post
Share on other sites

Next little tutorial on this really great module...

Getting a Footermenu stripped from the mainmenu and output in a footermenu

1. Setup Pagefield for your /settings/ or /tools/ page

This solution could be set by the user direct via a simple pagefield to select normal contentpages that a normaly in the mainmenu...but this items should stay not there and render somewhere else...

attachicon.giffootermenu.jpg

2. Strip the choosen pages from the mainmenu

//get the pagefield content
$footerMenu = $settings->footer_menu;

//just the needed option listed - other options like needed for your menu
$options = array(
       'selector' => 'id!='.$footerMenu.'',    // define custom PW selector
);

3. Output the pagefield items

attachicon.giffootermenusorting.jpg

<?php //in my example i am in the _main.php template and render some footermenu
if ($settings->footer_menu) { //check for the pagefield
    echo '<div class="eight columns">';
    foreach ($settings->footer_menu as $item) {
        if (!$item->is('hidden') || $item->is('unpublished')) { //check for hidden/unpublished pages!!
	    echo '<a href="'.$item->url.'" class="button sendbtn btnSend">'.$item->title.'</a>';
	}
    }
    echo '</div>';
}
?>

hope this is helpful...have fun.

I don't have a blog and i am a fan of keeping things together....so don't bother if i'm posting such things here and write me if such usecases are not helpful or nonsense...... ;)

Best regards mr-fan

There's a tutorial board here in the forums where you would post such examples. I don't think it helps much in here.

Share this post


Link to post
Share on other sites

The selector only is used to filter the children of the current nodes.

Thanks Soma, I understand better now.

Share this post


Link to post
Share on other sites

There's a tutorial board here in the forums where you would post such examples. I don't think it helps much in here.

Ok if i've the time i collect my snippets to a "Tipps&Tricks with MarkupSimpleNavigation" topic - to get things connected.

For myself - i always read modulethreat completly if i'm using them to get the best start or knowlegde about the used addon...Read the fucking Manual....is not a option it is a obligation if you are new to a cms/module/technique....;)

But i will follow your advice since it better fits as tutorials than tracking issues or getting help.

Best regards and thank you for feedback mr-fan

Share this post


Link to post
Share on other sites

Markup Simple Navigation Module

While there was a lot of people asking how to make navigation, and there were many examples around already (apeisa, ryan...) I took the chance to sit down 2-3 hours to make a simple navigation module. It has even some options you can control some aspects of the output.

Installation:

1. Put this module's folder "MarkupSimpleNavigation" into your /site/modules folder.

2. Go to your module Install page and click "Check for new modules". It will appear under the section Markup. Click "install" button. Done.

Technically you don't even need to install it, after the first load call ( $modules->get("MarkupSimpleNavigation") ) it will install automaticly on first request if it isn't already. But it feels better. However, it will not be "autoloaded" by Processwire unless you load it in one of your php templates.

Documentation:

https://github.com/somatonic/MarkupSimpleNavigation/blob/master/README.md

Modules Repository processwire.com

mods.pw/u

Download on github

https://github.com/somatonic/MarkupSimpleNavigation

Advanced example with hooks

creating a Bootstrap 2.3.2 Multilevel Navbar

https://gist.github.com/somatonic/6258081

I use hooks to manipulate certain attributes and classes to li's and anchors. If you understand the concept you can do a lot with this Module.

could i put the "creating a Bootstrap 2.3.2 Multilevel Navbar" code in the _init.php to run ?

Share this post


Link to post
Share on other sites

The load and setup hooks you could put in _init.php but the render would be where you render your navigation.

Share this post


Link to post
Share on other sites

Hi Soma,

I need to use plain PHP pages ("/foo/bar.php") to a website powered by ProcessWire. How can I add this in Markup Simple Navigation?

Sample structure that shows the mix of Processwire pages and PHP pages in use:

  • Products
    • Product 1
    • Product 2
    • Product 3
    • Request quote <- Quote form in plain PHP.
  • References
  • About us

Share this post


Link to post
Share on other sites

I need to use plain PHP pages ("/foo/bar.php") to a website powered by ProcessWire. How can I add this in Markup Simple Navigation?

Question: any particular reason why the form needs to be processed outside of PW?

Share this post


Link to post
Share on other sites

Wanted to ask the same.

Only way I see is use a page with a text field to enter a path to a php file. Then use that field as in the url tag in the item template. I.e. href='{pathphp|url}'

Share this post


Link to post
Share on other sites

@Mike Rockett & Soma: The reason for processing the form outside PW is that it's part of a 3rd party booking system using an external database.

I have the following enhancement request for the module: "It shall be possible to include plain PHP pages ("/foo/bar.php") from third party plug-ins and the like to MarkupSimpleNavigation. Suggested solution is to allow adding in-memory pages to the navigation entries on the fly. The purpose of adding in-memory pages is to place the 3rd party page in PWs Page structure. Something along these lines:

$p = new Page();
$p->setOutputFormatting(false);
$p->parent = $pages->get("/MyParentPage");
$p->template = "SomeTemplate";
$p->title = "External page";
$p->name = "external_page";
// Note I do not use $p->save(), so it's a pseudo page that exists in memory only.
// Add the page to $entries somehow [...] -> $entries = $pages->get("/")->navigation_entries; ....
$entries = $pages->get("/")->navigation_entries; // Load entries (including our new page)
echo $treeMenu->render(null, null, $entries);

(Perhaps this is already possible without making changes to the module?)

Right now, I will go ahead and 1) make a unique template for the form that "include()" the form from a PHP file, 2) add a PW page for the form and 3) repeat this process for all relevant files (4 times in total per site using the booking system). I can then output a link and title to the page using MarkupSimpleNavigation. (I have done this before, it's a bit cumbersome but it will work.)

Share this post


Link to post
Share on other sites

While it would be possible to inject in memory pages, I don't want to go that route for various reasons.

What I do is add a dummy page as you mention. Then add a field "exturl" to the template. Then enter the url in that field like "/path/to/file.php".

In the navigation code you then use "exturl" in the item_tpl:

$options = array("item_tpl" => "<a href='{exturl|url}>{title}</a>');
  • Like 1

Share this post


Link to post
Share on other sites

I have problems with outputting the whole Menu Tree  up to the parent page Product when in 3rd level of a submenu.

The structure is

Home

  Products

      product 1

      product 2

         Item 1

         Item 2

         Item 4

      product 3

My menu looks like this

$leftNav = $modules->get("MarkupSimpleNavigation");

$leftNavOptions = array(
	'max_levels' => 3,
	'collapsed' => true,
	'show_root' => false,
	'levels' => true,
	'outer_tpl' => '<ul id="leftNavOuter" class="noPrint">||</ul>',
	'firstlast' => true
);

$root = $page->parents->count == 1 ? $page : $page->parent;
echo $leftNav->render($leftNavOptions, $page, $root);

that works perfect well until you navigate into the 3rd Level, then all levels above disapear and you see only the items level

Is there a way to output all three levels when in 3rd level?

Your help is much appreciated.

Share this post


Link to post
Share on other sites

that works perfect well until you navigate into the 3rd Level, then all levels above disapear and you see only the items level

I think your problem comes from this:

$root = $page->parents->count == 1 ? $page : $page->parent;

For the first two levels $root is "Products", but on the third level $root is "product 2" or whatever level 2 parent you are in. So you only see children of "product 2".

I think this will do what you want:

$root = $page->rootParent;

Share this post


Link to post
Share on other sites

Hello all,

I have two questions:

First:

using MSN Module ... is it possible to have "unlinked" items  in the menu?.
I have several pages that serve as headlines to structure the content better. They have a seprate template (menu-ueberschrift) and they should not be clickable ... just be there as plain text. These Items have children. I want an uncollapsed menu..

Second question:

Goes into the same direction: I would like to have active pages without links, too. I think there is no need for links  for a page I'm currently viewing.

Hopefully someone can help, as I'm stuck here.

Share this post


Link to post
Share on other sites

Solved it myself ...

Finally managed to set up hooks:

function myItemString(HookEvent $event) {
    $child = $event->arguments('page'); // current rendered child page
    // any logic with $child possible here
    
    /**
     * Überschriften
     */
    if ($child->template->name == 'menue-ueberschrift') {
        $event->return = "<span> $child->title</span>";
    };

    /**
     * active
     */
    if ($child->template->name == 'standard') {
        if ($child->id == wire('page')->id) {
            $event->return = "<span> $child->title</span>";

        }
    };
}

// setup the hook after on ___getItemString($class, $page) method
$nav->addHookAfter('getItemString', null, 'myItemString');
/* Render Navigation Markup adding options, too---------------- */
echo $nav->render()

Seems to work so I guess it's ok.

And I've learned something new :-)

Cheers Hanna

  • Like 2

Share this post


Link to post
Share on other sites

Ok,

I am having some issues with this menu (will be in bootstrap).

I currently have:

		<?php
			$nav = $modules->get("MarkupSimpleNavigation");

	 function hookGetListClass(HookEvent $event){
	    $child = $event->arguments('page'); // current rendered child page
	    $class = $event->arguments('class'); // the class string already present
			$options = array(
				'has_children_class' => 'dropdown',
				'outer_tpl' => '<ul class="nav">||</ul>',
				'inner_tpl' => '<ul class="dropdown-menu">||</ul>'
			);


	     // any check on that child to set your custom class
	     if($child->id == 1001){
	         $event->return .= " inner"; // add your custom class to the existing
	     }
	 }

	 // setup the hook after on ___getListClass($class, $page) method
	 $nav->addHookAfter('getListClass', null, 'hookGetListClass');
	 echo $nav->render($options);
	 ?>

However, the nav doesnt seem to be taking the options, and is instead using the "has_children" class. I saw a post almost exactly like this, but couldnt get the outcome. The inner ul should have a class of "dropdown".

Share this post


Link to post
Share on other sites

Hey guys,

I love this plugin. It works almost perfect for me. But I have a little question. I want that the parents don´t have a link.

This is my markup now:
<nav>
               
        <a href="<?=$config->urls->root; ?>" id="home">Von der Berkelaue</a>
       
        <?php 
            $treeMenu = $modules->get("MarkupSimpleNavigation"); // Modul laden   

        
            $options = array(
                'max_levels' => 2,
                'show_root' => true,
                'outer_tpl' => '<ul id="menu">||</ul>',
                'inner_tpl' => '<ul>||</ul>',
                'list_tpl' => '<li%s>||</li>',
                'item_tpl' => '<a href="{url}">{title}</a>',
                'item_current_tpl' => '<a href="{url}" class="active">{title}</a>',
                'debug' => true,
                'selector' => 'template!=aktuelles-eintrag, has_parent!=/galerie/',
            );
            echo $treeMenu->render($options); // Navigation rendern
        ?>
    </nav>

And this is my output:

<ul id="menu">
    <li class="current"><a href="/" class="active">Startseite</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li><a href="/aktuelles/">Aktuelles</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li class="has_children"><a href="/uber-uns/">Über uns</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
        <ul>
            <li><a href="/uber-uns/familie/">Familie</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/uber-uns/unsere-hunde/">Unsere Landseer</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
        </ul>
    </li>
    <li><a href="/wurfplanung/">Wurfplanung</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li class="has_children"><a href="/wurfe/">Würfe</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
        <ul>
            <li><a href="/wurfe/wurfplanung/">A-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/b-wurf/">B-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/c-wurf/">C-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/d-wurf/">D-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/e-wurf/">E-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/f-wurf/">F-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/g-wurf/">G-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
        </ul>
    </li>
    <li><a href="/galerie/">Galerie</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li><a href="/gastebuch/">Gästebuch</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li><a href="/links/">Links</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li><a href="/kontakt/">Kontakt</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
</ul>
<!-- [MarkupSimpleNavigation Rendertime: 0.0366] -->
</nav>

And it should look like this:

<ul id="menu">
    <li class="current"><a href="/" class="active">Startseite</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li><a href="/aktuelles/">Aktuelles</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li class="has_children"><a href="#">Über uns</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
        <ul>
            <li><a href="/uber-uns/familie/">Familie</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/uber-uns/unsere-hunde/">Unsere Landseer</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
        </ul>
    </li>
    <li><a href="/wurfplanung/">Wurfplanung</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li class="has_children"><a href="#">Würfe</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
        <ul>
            <li><a href="/wurfe/wurfplanung/">A-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/b-wurf/">B-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/c-wurf/">C-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/d-wurf/">D-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/e-wurf/">E-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/f-wurf/">F-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
            <li><a href="/wurfe/g-wurf/">G-Wurf</a>
                <!-- [Selector Level2: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
            </li>
        </ul>
    </li>
    <li><a href="/galerie/">Galerie</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li><a href="/gastebuch/">Gästebuch</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li><a href="/links/">Links</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
    <li><a href="/kontakt/">Kontakt</a>
        <!-- [Selector Level1: template!=aktuelles-eintrag, has_parent!=/galerie/] -->
    </li>
</ul>
<!-- [MarkupSimpleNavigation Rendertime: 0.0366] -->
</nav>

I hope you understand my problem. My english isn´t very good :D

Thanks, Jannis

Share this post


Link to post
Share on other sites

Hi Jannis,

Is  that you want this

<li class="has_children"><a href="#">Über uns</a>

appear like a headline ? with no link?

I think it would be a good idea to give those 'headlines' their own template like 'ueberschrift' or so.

Then you could proceed similar to what I posted here using a hook.

  • Like 1

Share this post


Link to post
Share on other sites

Hi,

i am using this Module for quit a while with great joy. Easy to implement and very flexible.

Right now i wonder if something like this is possible by using just the module options without hooking the core: I have a 2-level menu with several parents and relevant child pages. On last parent the child array is (should) populated from a variable ($items), which collects only (child) pages with dedicated page view permission.

So i fiddle around with the "selector_levelN" option. First try:

$options = array(
  "levels" => true,
  "max_levels" => 2,
  "selector_level2" => "id=$items"
);

This gives me the expected result in level2 on the last child tree, but fails of course because the selector ("id="$items") returns nothing on all other parent/child levels. What i would need is some condition like (Tried this and other notations, no luck):

$options = array(
...
"selector_level2" => (parent=<id>) ? "id=$items" : ""
);

In words. If the parent page has a dedicated ID (here: the parent page which holds all permission controlled (child) pages), populate the "selector_level2" option with predefined pages ($items), otherwise don't constrain the collecting for level 2.

Probably this is the nutshell: Are condition possible/allowed in module option selector values?

cheers and thanks

felic

Share this post


Link to post
Share on other sites

You could perhaps use OR-groups in the selector (untested):

$options = array(
  "level" => true,
  "max_levels" => 2,
  "selector_level2" => "(parent!=$lastpageid), (id=$items)"
);
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By eelkenet
      Hi! I've created a small Inputfield module called InputfieldFloatRange which allows you to use an HTML5 <input type="range" ../> slider as an InputField. I needed something like this for a project where the client needs to be able to tweak this value more based on 'a feeling' than just entering a boring old number. Maybe more people can use this so I'm hereby releasing it into the wild.  
       
      What is it?
      The missing range slider Inputfield for Processwire. 
      What does it do?
      This module extends InputfieldFloat and allows you to use HTML5 range sliders for number fields in your templates.
      It includes a visible and editable value field, to override/tweak the value if required.  
      Features
      Min/max values Precision (number of decimals) Steps (Read more) Manual override of the selected value (will still adhere to the rules above) Usage
      Clone / zip repo Install FieldtypeFloatRange, this automatically installs the Inputfield Create new field of type `Float (range)` or convert an existing `Float`, `Integer` or `Text` field. To render the field's value simply echo `$page->field` Demo
      A field with Min=0, Max=1, Step=0.2, Precision=2

      Field with settings Min=0, Max=200, Step=0.25, Precision=2

       
      Todo
      Make the display-field's size configurable (will use the Input Size field setting)  Hopefully become redundant If it's usable for others I'll add it to the Modules list  
      Changelog
      v002
      - Fix issue where setting the step value to an empty value created problem with validation
      - Make the display-field optional 
      v001
      - Initial release
       
      Thanks!
       
       
    • By Robin S
      Another little admin helper module...
      Template Field Widths
      Adds a "Field widths" field to Edit Template that allows you to quickly set the widths of inputfields in the template.

      Why?
      When setting up a new template or trying out different field layouts I find it a bit slow and tedious to have to open each field individually in a modal just to set the width. This module speeds up the process.
      Installation
      Install the Template Field Widths module.
      Config options
      You can set the default presentation of the "Field widths" field to collapsed or open. Field widths entered into the Template Field Widths inputfield are only applied if the Edit Template form is submitted with the Template Field Widths inputfield in an opened state. "Collapsed" is the recommended setting if you think you might also use core inputs for setting field widths in a template context. You can choose Name or Label as the primary identifier shown for the field. The unchosen alternative will become the title attribute shown on hover. You can choose to show the original field width next to the template context field width.  
      https://github.com/Toutouwai/TemplateFieldWidths
      https://modules.processwire.com/modules/template-field-widths/
    • By adrian
      Tracy Debugger for ProcessWire
      The ultimate “swiss army knife” debugging and development tool for the ProcessWire CMF/CMS

       
      Integrates and extends Nette's Tracy debugging tool and adds 35+ custom tools designed for effective ProcessWire debugging and lightning fast development
      The most comprehensive set of instructions and examples is available at: https://adrianbj.github.io/TracyDebugger
      Modules Directory: http://modules.processwire.com/modules/tracy-debugger/
      Github: https://github.com/adrianbj/TracyDebugger
      A big thanks to @tpr for introducing me to Tracy and for the idea for this module and for significant feedback, testing, and feature suggestions.
    • By adrian
      This module allows you to automatically rename file (including image) uploads according to a configurable format
      This module lets you define as many rules as you need to determine how uploaded files will be named and you can have different rules for different pages, templates, fields, and file extensions, or one rule for all uploads. Renaming works for files uploaded via the admin interface and also via the API, including images added from remote URLs.   Github: https://github.com/adrianbj/CustomUploadNames
      Modules Directory: http://modules.processwire.com/modules/process-custom-upload-names/
      Renaming Rules
      The module config allows you to set an unlimited number of Rename Rules. You can define rules to specific fields, templates, pages, and file extensions. If a rule option is left blank, the rule with be applied to all fields/templates/pages/extensions. Leave Filename Format blank to prevent renaming for a specific field/template/page combo, overriding a more general rule. Rules are processed in order, so put more specific rules before more general ones. You can drag to change the order of rules as needed. The following variables can be used in the filename format: $page, $template, $field, and $file. For some of these (eg. $field->description), if they haven't been filled out and saved prior to uploading the image, renaming won't occur on upload, but will happen on page save (could be an issue if image has already been inserted into RTE/HTML field before page save). Some examples: $page->title mysite-{$template->name}-images $field->label $file->description {$page->name}-{$file->filesize}-kb prefix-[Y-m-d_H-i-s]-suffix (anything inside square brackets is is considered to be a PHP date format for the current date/time) randstring[n] (where n is the number of characters you want in the string) ### (custom number mask, eg. 001 if more than one image with same name on a page. This is an enhanced version of the automatic addition of numbers if required) If 'Rename on Save' is checked files will be renamed again each time a page is saved (admin or front-end via API). WARNING: this setting will break any direct links to the old filename, which is particularly relevant for images inserted into RTE/HTML fields. The Filename Format can be defined using plain text and PW $page variable, for example: mysite-{$page->path} You can preserve the uploaded filename for certain rules. This will allow you to set a general renaming rule for your entire site, but then add a rule for a specific page/template/field that does not rename the uploaded file. Just simply build the rule, but leave the Filename Format field empty. You can specify an optional character limit (to nearest whole word) for the length of the filename - useful if you are using $page->path, $path->name etc and have very long page names - eg. news articles, publication titles etc. NOTE - if you are using ProcessWire's webp features, be sure to use the useSrcExt because if you have jpg and png files on the same page and your rename rules result in the same name, you need to maintain the src extension so they are kept as separate files.
      $config->webpOptions = array(     'useSrcExt' => false, // Use source file extension in webp filename? (file.jpg.webp rather than file.webp) ); Acknowledgments
      The module config settings make use of code from Pete's EmailToPage module and the renaming function is based on this code from Ryan: http://processwire.com/talk/topic/3299-ability-to-define-convention-for-image-and-file-upload-names/?p=32623 (also see this post for his thoughts on file renaming and why it is the lazy way out - worth a read before deciding to use this module). 
       
       
      NOTE:
      This should not be needed on most sites, but I work with lots of sites that host PDFs and photos/vectors that are available for download and I have always renamed the files on upload because clients will often upload files with horrible meaningless filenames like:
      Final ReportV6 web version for John Feb 23.PDF

×
×
  • Create New...