Jump to content

FieldtypeKeyValueMultiple need help while developing


Can
 Share

Recommended Posts

Hola amigos :)

I'm quite excited about this one and really wanted to finish it myself or at least come with "bigger" problems^^

But now I need some advice as I'm stuck on some basics. The module is derived from FieldtypeEvents by Ryan.

What it does

It's a customizable two column table for key value pairs. Right now customization is about changing columns headings and column width.

Customizing sorting of the rows is planned, and the input is already there but not yet working. Drag and drop sorting is planned for the future two. Translatable column headings are almost there, but I got the same issue like with the MarkupCookieConsent, so only default language is saving..and many more features are planned (and hopefuly will be included in the future) except those it's already working quiet nice.

So when editing a page you can add/remove columns (ah they got sorted alphabetical at the moment).

screenshot.png

What it doesn't (And that's for me the most important part right now!)

The API side is lacking remove of single and all rows capabillities, which I personally need right now because I build a small front end page edit screen to let users edit some data. I got the whole key/value input running adding rows on the fly like in the backend, but I'm not able to delete anything at all.. ^^

What I tried

After digging and digging I realised that FieldtypeEvents is really basic and KeyValueArray.php (former EventArray.php) is missing some methods. At least I thought because some other modules are implementing there own remove() methods, even if I thought it should inherit all methods because it extends WireArray, I decided to copy some code without luck, trying to understand and alter as needed (seems really easy to understand) but still no luck at all.

While writing this I thought it might be interesting to review the thread from Ryan about his FieldtypeEvents and found that someone just managed to remove an event by using remove($event) without changing Ryans code. So digging again trying some things, stumbling over public function removeQuietly($key) and finally this one deleted the row! :D

So for now I just copied removeQuietly method into KeyValueArray.php and renamed it to remove($key) and it's working.

But I would love to know why all this? Why didn't remove just work from WireArray.php? And why the copied methods still didn't worked?

I tend to write and write and write haha..

Use cases

What is all this useful for?

Consider a contact template, you could use this field to store all contact information like so

contact-template.png

Some minutes ago I had some more examples but it's gettin late..

Ah

Know that you can (like with FieldtypeEvents) not only foreach the whole table, but get them row by row:

$row = $page->keyValues->get("Responsible");
$label = $row->key;
$value = $row->value;
Just noticed that the contact example would be even better with another sorting than alphabetical to just iterate the whole table

But you could already get them line by line as needed ;-)

Kay that's it for now.

  • Like 1
Link to comment
Share on other sites

Tried to get translatable config fields working, but no luck so far.

I used to build them using

public function ___getConfigArray() {
    return array(
    'keyLabel' => array(
    'type' => 'text',
    'label' => $this->_('Label for key field'),
    'value' => '',
    'notes' => $this->_('Defaults to "Key"'),
    'columnWidth' => $this->keyWidth,
    'useLanguages' => true
    )
};
 
Thought maybe it's not yet fully supporting "useLanguages" so I switched to getConfigInputfields which other modules from Ryan are also working with languages..
public function ___getConfigInputfields() {
 $inputfields = $this->wire(new InputfieldWrapper());

 $f1 = $this->modules->get("InputfieldText"); 
 $f1->attr('name', 'keyLabel');
 $f1->label = $this->_('Label for key field');
 $f1->attr('value', ''); 
 $f1->attr('size', 100); 
 $f1->notes = $this->_('Defaults to "Key"');
 $f1->columnWidth = $this->keyWidth;

 if($this->wire('languages')) {
    $f1->useLanguages = true;
    foreach($this->wire('languages') as $language) {
    if($language->isDefault()) continue; 
    $f1->set("value$language", ''); 
 }
}
$inputfields->add($f1); }

But than nothing is getting saved at all. Before at the default language value would save..

In init() I'm having

$this->set('keyLabel', 'Key');

if($this->wire('languages')) foreach($this->wire('languages') as $language) {
    // account for alternate formats in other languages
    /** @var Language $language */
    if($language->isDefault()) continue; 
/* tried with and without underscores */
    $this->set("keyLabel__$language", $this->get("keyLabel__$language"));
  //  $this->set("keyLabel$language", $this->get("keyLabel$language"));}

Still no luck.  :(

Link to comment
Share on other sites

hey this is great and was something i was eventually going to work on also, because it would be good to have a dedicated fieldtype for key value where they are entered into fields like this. Currently i use tables for this, and here is a thread with some other info, possible use cases;

one thing that might be cool would be to have another column as in Martijn's example, so there is a name (key) label and value, and in his example there is also a trick for hiding the name of the keys from non super users (so they wouldn't break the front end in case they changed an in-use variable)

https://processwire.com/talk/topic/8373-use-delimited-texarea-table-or-yaml-for-settings/

  • Like 2
Link to comment
Share on other sites

Nice to hear this from you Macrura! I know your thread about delimited textareas and startet with such myself. Even though I read the thread I didn't know about TextformatterMultiValue until Martijn mentioned your thread and the Textformatter in a pm couple of hours ago.

Maybe I should consider a to split key into key/label. Maybe only if desired..

When I started the module I named the db columns the same and ran into issues so I just renamed the key column to label. Maybe it's not good/allowed to call a column "key" or maybe it was about the index? Just reread and noticed that you wrote name. Of course the db column doesn't has to be called key...

I will go through this thread again and see if I find more interesting/useful ideas/use cases.

Thanks to you and Martijn for mentioning!

Saludos Can

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

×
×
  • Create New...