Jump to content

How to create a field with multiple text entries


baymarketingco
 Share

Recommended Posts

Hi,

I can't get my head around this simple concept. What am I missing?

I need a field that allows me to enter multiple lines of text. Let's take some JSON+LD input as an example:

The alternateName field allows one to enter more than one name for a hotel. So I need a field that allows me to say, click a + button to add a new option for an alternate name, add the text, and then do so again, if I have another alternate name. This then obviously has to generate the proper code automatically. I'm thinking this would be an array???

I can't seem to find a solution. Perhaps it's a lack of understanding what I'm looking for.

Another, somewhat related issue: How do I create HTML that only outputs when there is something to output?

Let's take the same JSON+LD. Here's the code for that:

<script type="application/ld+json">
"alternateName": "<?php echo $page->json_ld_alternateName; ?>",
</script>

Now, is there a way to REMOVE the whole alternatename line of code (or not make it show in the first place), if there is no json_ld_alternateName inserted by the user?

Thanks!

Link to comment
Share on other sites

<?php
// only render output if there is something set in field json_ld_alternateName
if(strlen($page->json_ld_alternateName) > 0) {
  echo "  <script type='application/ld+json'>
     'alternateName: '{$page->json_ld_alternateName}',
  </script>\n";
}

I wrap "doublequotes" around the php string and use 'singlequotes' for strings within the markup. If you run into situations where you also would need doublequotes within the markup, you can use them if you escape them with a backslash:

<?php
echo "<p>A string with 'singlequotes' and escaped \"doublequotes\"!</p>"; 
// will output: <p>A string with 'singlequotes' and escaped "doublequotes"!</p>

 

  • Like 4
Link to comment
Share on other sites

That is awesome. Thanks @horst! Works a charm.

Now the other issue, if you could help. How do I create a field where I can enter one or more lines of text? In other words, stuff to populate json_ld_alternateName.

I looked at the repeated field, but that seems to require info that's already present in the system. Likewise, the pagetable field requires something too.

What I need is a field that allows me to add the alternate name, then add another, if applicable.

Thanks!

Link to comment
Share on other sites

1 hour ago, blommie said:

I looked at the repeated field, but that seems to require info that's already present in the system. Likewise, the pagetable field requires something too.

Hi,

What is wrong with repeaters in your case? Can you please elaborate?

  • Like 1
Link to comment
Share on other sites

Hi @szabesz,

Thanks for your reply.

Perhaps I don't understand how to set up a repeater field properly.

If I use a repeater, I get an option to choose from a predefined list of choices.

What I require is a field where I populate the text myself, with an option to add more text fields. Check the attached image to see what I'm aiming for. Hopefully it makes more sense than my words :)

 

repeatable_text_input.png

Link to comment
Share on other sites

I know that using a repeater for such an input looks like a bit convoluted solution, because a repeater is designed to handle more than one field "during input", but if you only assign one filed to the Repeater you use, then you can get a similar behavior. With a little bit of admin CSS hacking, you might even be able to tweak it to make its UI less "verbose" (on the repeater field's Details tab settings you can even set its items to be "always open").

Note, that without ajax loading, you might not want to create more than 40-60 items for performance reasons (depending on your server config), see:

This has been improved with the “ajax option” though:
https://processwire.com/blog/posts/more-repeaters-repeater-matrix-and-new-field-rendering/

  • Like 1
Link to comment
Share on other sites

11 hours ago, blommie said:

How do I create a field where I can enter one or more lines of text?

Besides Repeater and PageTable, there are Table and Multiplier in the ProFields package that can be used for repeating content.

But for a simple repeatable text string you could just use a textarea field and explode() on newline to get an array of values.

$values = explode("\n", str_replace("\r", '', $page->my_textarea));

 

  • Like 1
Link to comment
Share on other sites

@blommie: I'm not sure JsonNativeField is going to fit your bill. While it supports an arbitrary number of entries, those are key => value pairs, much like regular fields with a name and value.

You might want to look at the ChosenSelect Inputfield, which behaves as a tagging field and allows creating new "tags" (which are actually PW pages behind the scenes with the tag value as the title) on the fly if you enter a new name.

Repeater and Page field types don't need you to have the pages used in them to already be present in the system. A page is just an entry in a table in the database, and if you use repeaters, the template configuration for these pages is even done behind the scene when you define the repeater field's parts (fields). If you're coming from other systems, the concept of using "full-blown" pages for simple data might seem a bit much, but because PW keeps things simple under the hood, it actually doesn't get much leaner database-wise.

  • Like 2
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...