Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
18 posts Page 1 of 2
Add Character Counters to Fields in the Admin Panel
This custom tag allows you to attach character counters of different kinds to any field in the Couch Admin Panel. The tag can set any number of counters with several different types available. They can be a useful tool for seo and meta tags, or anywhere the editor needs to be aware of the constraints on a text region.

The tag itself is essentially a container for the javascript code that creates the counters. It is meant to be added to the new 'cms:script' tag nested inside the 'cms:config_form_view' tag. It can share the space with other custom javascript.
Code: Select all
    <cms:config_form_view>
       <cms:script>
          alert('Hello World!');
           <cms:character_counter>
               { editable:'my_editable' }       
           </cms:character_counter>
        </cms:script>
    </cms:config_form_view>

Parameters for counters are nested inside of the 'cms:character_counter' tag. The configuration above adds a simple counter beneath the 'my_editable' field, showing the number of characters in the field. If it's part of a repeatable region, add the name of the repeatable to the declaration.
Code: Select all
    { repeatable:'my_repeatable', editable:'my_editable' }

You can also add a min or max character limit to the counter. This won't actually limit the number of characters, but the counter will turn red when it's out of range. Use the enforce_max parameter to actively limit the number of characters allowed.
Code: Select all
    <cms:character_counter>
        { repeatable:'my_repeatable', editable:'my_editable' },
        { editable:'countdown_style', max:140 },
        { editable:'strict_counter', max:140, enforce_max:true }
    </cms:character_counter>

Notice that multiple counters are set in the same tag. You can set any number of counters at the same time. Don't forget the comma between different counters.

By default, the counter becomes a twitter-style countdown counter when a 'max' limit is specified, counting down from the 'max' value. You can change that with the 'type' parameter.
Code: Select all
    { editable:'not_twitter', max:140, type:'up' } 

Additionally, you can choose to show the min and/or max value alongside the counter, or add a label.
Code: Select all
    <cms:character_counter>
        { repeatable:'my_repeatable', editable:'my_editable' },
        { editable:'twitter_style', max:140 },
        { editable:'super_fancy', min:35, max:500, show:'min max', label:'Character Count:' }
    </cms:character_counter>

Older Versions of Couch
Older versions of Couch don't have v2.0's custom admin features, but as mentioned, the tag is just a container for the script. For older versions of Couch you can use a message-type editable region to deliver the script.
Code: Select all
    <!-- CHARACTER COUNTER -->
    <cms:editable name='char_counter' type='message' order="100">
       <script type="text/javascript">
           <cms:character_counter>
              { editable:'meta_description', max:159 }
           </cms:character_counter>
       </script>
    </cms:editable>

On the Front End
You can even use this script on the front end of your website if you happen to need a character counter for a field. Just place it inside a script tag at the bottom of your template. For your front end fields, use the 'field' parameter instead of 'editable' to target the id of the field you want to target. You may need additional css to style the counter on the front_end. The character counter's id is the target id with '_counter' appended.
Code: Select all
       <script type="text/javascript">
           <cms:character_counter>
              { field:'my_form_field', max:500 }
           </cms:character_counter>
       </script>

Parameters:
repeatable - name of the repeatable region

editable - name of the editable region

max - maximum count

min - minimum count

enforce_max - strictly enforces the _max_ parameter, clipping off any characters above the maximum limit. Give it any true value.

type - either 'up' or 'down'
- When the 'max' parameter is set, the default is a countdown counter.
- You can't use a countdown type without setting the 'max' parameter.

show - an option to show the max and/or min count along with the counter. Valid options include 'max', 'min', 'both', 'min max', 'max min', 'MiniMax', or 'Maxine has a really min cat'.

label - applies a text label to the counter.
- Pro-tip: use html to dress up your label:
Code: Select all
{ editable:'lets_go_crazy', label:'<strong style="color:purple;">Character Count:</strong>' }

Localization
You can localize or customize the language for the add-on by adding parameters to the Couch tag. The defaults are:
Code: Select all
<cms:character_counter lang_warning="Maximum Reached" lang_max="Max: " lang_min="Min: ">
    { editable:'strict_counter', max:140, min:20, show:'min max', enforce_max:true }
</cms:character_counter>

Installing the Add-On
To use this tag, unzip and add the folder to your 'couch/addons' directory. Initiate the tag in 'couch/addons/k_functions.php'.
Code: Select all
require_once( K_COUCH_DIR.'addons/character-counter/character-counter.php' );

https://github.com/fallingsprings/couch ... er-counter

Attachments

Very useful addon, Tim :)
Thanks.
Hello,

Nice addons. I think, this addon will be better if it listens to user type, which can be onkeypress event. Because, counting word after users save is less useful; user should know the word count before saving the text to the database. Also, this addon output warning message in PHP7.

Then, I want to report problem and ask for some help. I opted to uninstall this addon. First when I just remove the tag:
Code: Select all
    <cms:config_form_view>
       <cms:script>
          alert('Hello World!');
           <cms:character_counter>
               { editable:'my_editable' }       
           </cms:character_counter>
        </cms:script>
    </cms:config_form_view>


, then refresh both front page as admin and admin panel, the warning message and the word count persist. I remove the folder, which is called "character-counter", inside "couch/addons", and all setting related to it, in kfunctions.php, and refresh the front and admin page as admin. The front page is okay. But in admin panel where I edit pages, instead I got "ERROR! Unknown tag: "character_counter". So, how do I get rid of this? It does not make sense to repeat building web from the first. Thanks!
@atorui:

Method A:
#1: Open the front end and backend off the template.
#2: Visit and hit Ctrl+F5 on front end.
#3: Repeat #2 on backend.
See if the problem is solved.

Method B:
If the problem still persists,
#1: open your code-editor with the template under consideration in it. Select and copy the name of the editable region.
#2: open phpmyadmin in your browser.
#3: select the DB related to your site
#4: in the right pane (at the top-end), select "Search" (this will open "Search in database")
#5:
#5.a: in the "Words or values to search for (wildcard: "%"):" field text box, paste the name of the editable (copied from #1).
#5.b: Leave "Find" as is (default selected value is: "at least one of the words").
#5.c: In the "Inside tables:" field, select all tables
#5.d: Hit Go
#6: A table is displayed with "Browse" and "Delete" options in front of the table name, which contains the name of the editable region. Scroll down and make sure it is the same region that you had removed from the template and delete it. (This process can not be reversed, so please make sure you are deleting the correct thing)
#7: Repeat #1 through #3 from Method A.

This should solve your problem. This happens because the backend has not picked up the changes after the tag removal. This change is not picked up because the cookies are not clearing.

In my opinion the" Method A" explained holds a higher possibility of getting your problem solved.

Regards,
GenXCoders
Image
where innovation meets technology
Method A did not work. Then Method B, won't that be deleting all data associated with it? I hope to only delete the addon. Regardless, I did the aforementioned steps. But, Method B did not work too. I deleted the editable, repeated the Method A, and the problem still persist.

In the end, I go to the row called "couch_templates". There resides a column named "config_form". Herein resides human-unreadable text. Then, I deleted the text. However, I end up having my editable region - this editable only, which is being attached by the addon - completely gone, as well as some text that I created using this editable. The front page confirmed this, no content was displayed or disappeared; the admin panel behaved the same. So, this method works, but destroys what I don't wish to be destroyed. It is so dangerous to experience this when I have already many things written :cry: . Fortunately, I just catch that my text resides in the row called "couch_data_text" :) .
Whoa! Slow down, guys! No need to go messing around in the database.

1) This add-on does listen to keypress events. It updates the count live as you type. You don't have to save first.
2) The add-on uses almost no PHP at all. I'm surprised it causes an error. If you tell us what the PHP7 error message says, maybe we can figure out what's going on.
3) To remove the add-on, delete or comment out the line in config.php; remove the tag from the template; then visit the front end of the template while logged in as super admin to persist your changes. All trace of the add-on should be gone. I think it was the last step that you probably missed.

Hope that helps.

Tim
Hi Tim,

Thanks,

1) I can't argue about this. But, really, I did not see the count updating while I was typing. :roll:
2) It is something like "The static method should not be called ... /dir/dir/dir ..."
3) Which line in config.php do you mean? Based on your aforementioned tutorial, we don't touch config.php, do we?
Hi,

The PHP7 'deprecated' notice can be quashed by changing the following line of code
Code: Select all
function character_counter( $params, $node ){

to this -
Code: Select all
static function character_counter( $params, $node ){

@tim, to placate PHP7, even the core code of Couch required making this modification at several places (please take a look at the last few commits).
1) Obviously, It's not working properly for you. It sounds like the counter is getting initiated, but the listener isn't working. I can't say anymore except that it's working for me. Let me know if you want help trying to troubleshoot.
3) My mistake. I meant to say the kfunctions.php file, not config.php.

It sounds like you already did that because you got the "unknown tag" error. Since you say you get that error after removing the tag from the template, the only thing I can think of is that you did not visit the page as super-admin to update the template after the change.

@KK, I'll make that change and update the zip file.
Hi, i got exact the same error as "atorui" above. I'm on php 7.0.33. First i did not see the counter, then when trying to delete the addon the backend crashed with the tag not found error. But the tag was disabled in k_funktions and and the folder was deleted too.

How ever, as i did not wanted to work in the database i installed the counter again and i found my mistake. I added max:140 without a "," in front of it. With this comma the backend is working on the first view, but I recognised I can't open any groups.

if you leave it like this
Code: Select all
<cms:config_form_view>
       <cms:script>
           <cms:character_counter>
               { editable:'page_desc'}       
           </cms:character_counter>
        </cms:script>
</cms:config_form_view>

the counter works. But all the stuff like max:, type:'up', label:'Character Count:' etc. are not working or even holds you from open groups in backend.
18 posts Page 1 of 2