i've searched for a solution in a php snippet or module, and so far i can't find anything, but i'm sure someone has needed this feature before.

i want my users to be able to create an unordered list of items without having to enter any html tags. i have created a cck field where they can input the items. i was thinking they could put each new list item on a new line, or perhaps separate with a special character to be replaced (by some clever php) to create the list.

does anything like this exist already?

any help would be most appreciated. i'm using drupal 5 and i have both the cck and contemplate modules installed.

Comments

megg’s picture

i still didn't find any module or snippet that does this, so this is how i solved my problem:

	// Order of replacement
	$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
	$order   = array("\r\n", "\n", "\r");
	$replace = '</li><li>';
	// Processes \r\n's first so they aren't converted twice.
	$newstr = str_replace($order, $replace, $str);
	$close_ul = '</ul>';
	$wrapstr = '<ul><li>' . $newstr . $close_ul;
	// if the last item has a converted linebreak after, remove it
	$last1 = '</li><li></ul>';
	$cleanstr = str_replace($last1, $close_ul, $wrapstr);
	// close the list correctly
	$last2 = '</li></ul>';
	$liststr = str_replace($close_ul, $last2, $cleanstr);
	echo 'liststr = ' . $liststr;

i added the $last1 and $last2 replacements to account for two scenarios:
1. the user puts a linebreak after the final list item, so it has to be removed before we close the list;
2. the user doesn't press enter after the final list item, so it doesn't have to be removed and we just have to close the list.

i added this code in my contemplate for this particular content type. this is what it looks like in contemplate:

<div class="field field-type-text field-field-your-field">
  <h3 class="field-label">Your field name</h3>
  <div class="field-items">
    <?php foreach ((array)$field_your_field as $item) { ?>
      <div class="field-item">
         <?php
		// Order of replacement
		$order   = array("\r\n", "\n", "\r");
		$replace = '</li><li>';
		// Processes \r\n's first so they aren't converted twice.
		$newstr = str_replace($order, $replace, $item['view']);
		$close_ul = '</ul>';
		$wrapstr = '<ul><li>' . $newstr . $close_ul;
		// if the last item has a converted linebreak after, remove it
		$last1 = '</li><li></ul>';
		$cleanstr = str_replace($last1, $close_ul, $wrapstr);
		// close the list correctly
		$last2 = '</li></ul>';
		$liststr = str_replace($close_ul, $last2, $cleanstr);
		echo $liststr;
	   ?>
       </div>
    <?php } ?>
  </div>
</div>

now, this all works for me, but if anyone has any comments regarding this, be my guest. is the contemplate area the best place for this code, or perhaps it could also go in the node.whatever.tpl.php file?

also, it does seem like a pretty big lump of code, so if any kind-hearted php-er out there can offer any advice on how this could be improved (by using a clever regular expression, perhaps?) let me know.

Anonymous’s picture

Is there not a CCK module that would built unordered lists with field entry?

Danny

Anonymous’s picture

Just completed a thorough search for a module to do this and nothing. I'll keep my fingers crossed that someone may inform us of such a module.

Anonymous’s picture

I found a solution!

1. Go to your Content Type.
2. Add feild to your content type.
3. Under Text select Check boxes/radio buttons.
4. Click Create feild
5. Check Multiple values
6. Enter all your list items in Allowed values list seperating each one with a new line.
7. Create a style for this feild-item with the following CSS. display:list-item;

That's it! Works great for me. Let me know if you have any questions.

www.ss-interactive.com

Danny

leenwebb’s picture

(this is an old thread, but I just did this exact thing so I thought I'd share for future-searchers)

I made a text-box entry field that contains a list of zip-codes, one per line (important for me -- because that is about the level of sophistication my clients can handle). Then in my template for that content type, I break out the list into an array:

$zip_code_list = explode("\r\n", $node->field_valid_zips[0][view]);

I used the array for comparison (an in_array kinda thing) but one could just as easily foreach your way through it to make a list if you wanted to display them.

jafar104’s picture

I tried what this post recommended: http://drupal.org/node/475654

and it worked well.

Pay attention to where your div's end