# Panel: Sort tags on save

**URL:** <https://forum.getkirby.com/t/panel-sort-tags-on-save/1649>\
**Category:** Questions\
**Tags:** v2\
**Created:** [July 20, 2015, 9:19pm UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649 "2015-07-20T21:19:15Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobiasweh](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tobiasweh/32/13376_2.png) [@tobiasweh](https://forum.getkirby.com/u/tobiasweh)\
**Post date:** [July 20, 2015, 9:19pm UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/1 "2015-07-20T21:19:15Z")

</div>

Hi,

is it possible to save tags in the panel alphabetically on save? E.g. convert

```markdown
----

Tags: One Tag,Another Tag,My Tag,No Tag

----

```

to

```markdown
----

Tags: Another Tag,My Tag,No Tag,One Tag

----

```

automatically on save.

Bye,  
Tobi

---

<div class="post-metadata">

**Author:** ![flokosiol](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/flokosiol/32/468_2.png) [@flokosiol](https://forum.getkirby.com/u/flokosiol)\
**Post date:** [July 21, 2015, 8:19am UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/2 "2015-07-21T08:19:27Z")

</div>

Sounds like a perfect use case for [panels hooks](http://getkirby.com/docs/panel/hooks) in combination with the [page update method](http://getkirby.com/docs/cheatsheet/page/update).

I haven’t use the hook system so far, so this is just kind of a guess!? 😉

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 21, 2015, 8:23am UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/3 "2015-07-21T08:23:00Z")

</div>

Yes, a post update hook is the best way, sth like

```php
<?php
<?php
kirby()->hook('panel.page.update', function($page) {
  	if(isset($page->content()->data['tags'])) {
	  $tags = $page->tags()->split();
	  sort($tags);
	  $tags = implode(",",$tags);
	  try {
		  $page->update(array(
		    'tags' => $tags,
		    ));
		} catch(Exception $e) {
	  		echo $e->getMessage();
		}
	}	
	});

```

---

<div class="post-metadata">

**Author:** ![tobiasweh](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tobiasweh/32/13376_2.png) [@tobiasweh](https://forum.getkirby.com/u/tobiasweh)\
**Post date:** [July 21, 2015, 8:44am UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/4 "2015-07-21T08:44:06Z")

</div>

Thanks! That works great 😄

I replaced `sort` with `natcasesort` because the latter is caseinsensitive.

As far as I can see this won’t cause an error for pages that don’t have a `tags` field, does it?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 21, 2015, 8:54am UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/5 "2015-07-21T08:54:46Z")

</div>

I haven’t tested that but you could put the code within the hook function within an if condition, just to make sure.

Edit: With the code above, if the field does not exist, it will be created (so, every page you save, will get a tags field) and if there are no tags, the field will be empty. Maybe you should just limit the behavior to templates that have a tags field.

---

<div class="post-metadata">

**Author:** ![tobiasweh](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tobiasweh/32/13376_2.png) [@tobiasweh](https://forum.getkirby.com/u/tobiasweh)\
**Post date:** [July 21, 2015, 9:07am UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/6 "2015-07-21T09:07:42Z")

</div>

How can I test if a page has a field? Is `if($page->tags())` the right way?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 21, 2015, 9:09am UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/7 "2015-07-21T09:09:12Z")

</div>

I edited the code above but maybe what you suggested is enough. The above code checks if the key exists.

Edit: No,`if($page->tags())` does not work …

---

<div class="post-metadata">

**Author:** ![tobiasweh](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tobiasweh/32/13376_2.png) [@tobiasweh](https://forum.getkirby.com/u/tobiasweh)\
**Post date:** [July 21, 2015, 12:02pm UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/8 "2015-07-21T12:02:27Z")

</div>

Thanks again! It works fine … unfortunately there seems no Hook for the site options so I have to sort the site tags manually …

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 21, 2015, 12:07pm UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/9 "2015-07-21T12:07:33Z")

</div>

In my test environment this works for the site options as well, provided there is a tags field in your site blueprint

---

<div class="post-metadata">

**Author:** ![tobiasweh](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tobiasweh/32/13376_2.png) [@tobiasweh](https://forum.getkirby.com/u/tobiasweh)\
**Post date:** [July 21, 2015, 1:14pm UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/10 "2015-07-21T13:14:02Z")

</div>

Uuups … may fault … the field was named `keywords` …

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:17pm UTC](https://forum.getkirby.com/t/panel-sort-tags-on-save/1649/11 "2025-02-06T17:17:01Z")

</div>

Archived on February 6, 2025, 5:17pm UTC


