From 2ecb57ed6eb8e5dcd48f705a4462a2ee5a921cd8 Mon Sep 17 00:00:00 2001
From: Thorge Petersen <petersen@rz.uni-kiel.de>
Date: Fri, 30 Jun 2023 13:47:42 +0200
Subject: [PATCH] Enable editing of custom extra fields in the organization
 webform

---
 CHANGELOG.md                                  |  3 +-
 .../snippets/organization_form.html           | 59 +++++++++++++++----
 2 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6801c903..9897f229 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,7 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Added
 
 - Configuration variable `ckanext.odsh.min_tag_length` indicating minimum characters allowed for tag length.
-- Configuration variable `ckanext.odsh.max_tag_length` indicating maximum characters allowed
+- Configuration variable `ckanext.odsh.max_tag_length` indicating maximum characters allowed for tag length.
+- Editing of custom extra fields alongside the fixed fields in organizations form.
 
 ### Removed
 
diff --git a/ckanext/odsh/templates/organization/snippets/organization_form.html b/ckanext/odsh/templates/organization/snippets/organization_form.html
index c0bf3069..a3cd0db7 100644
--- a/ckanext/odsh/templates/organization/snippets/organization_form.html
+++ b/ckanext/odsh/templates/organization/snippets/organization_form.html
@@ -26,24 +26,57 @@
 
   {% endblock %}
   
-  
 
- 
-  {% set extras = h.get_address_org(data) %}
+  {% block custom_fields %}
+  <div data-module="custom-fields">
+
+    {% set extras = h.get_address_org(data) %}
 
-  {{ form.input_address('person','Ansprechpartner', value= extras.person , index=0, placeholder='', type='text', attrs={}) }}
- 
-  {{ form.input_address('street','Straße', value= extras.street, index=1, placeholder='', type='text', attrs={}) }}
+    {{ form.input_address('person','Ansprechpartner', value= extras.person , index=0, placeholder='', type='text', attrs={}) }}
+   
+    {{ form.input_address('street','Straße', value= extras.street, index=1, placeholder='', type='text', attrs={}) }}
+    
+    {{ form.input_address('location','Stadt', value=extras.location, index=2, placeholder='', type='text', attrs={}) }}
+    
+    {{ form.input_address('telephone','Tel.-Nr.', value=extras.telephone, index=3, placeholder='', type='text', attrs={}) }}
   
-  {{ form.input_address('location','Stadt', value=extras.location, index=2, placeholder='', type='text', attrs={}) }}
+    {{ form.input_address('mail','E-Mail', value=extras.mail, index=4, placeholder='', type='text', attrs={}) }}
+   
+    {{ form.input_address('web','Website', value=extras.web, index=5, placeholder='', type='text', attrs={}) }}
   
-  {{ form.input_address('telephone','Tel.-Nr.', value=extras.telephone, index=3, placeholder='', type='text', attrs={}) }}
+    {{ form.input_address('gnd','GND URI', value=extras.gnd, index=6, placeholder='https://d-nb.info/gnd/1136109587', type='text', attrs={}) }}
 
-  {{ form.input_address('mail','E-Mail', value=extras.mail, index=4, placeholder='', type='text', attrs={}) }}
- 
-  {{ form.input_address('web','Website', value=extras.web, index=5, placeholder='', type='text', attrs={}) }}
-
-  {{ form.input_address('gnd','GND URI', value=extras.gnd, index=6, placeholder='https://d-nb.info/gnd/1136109587', type='text', attrs={}) }}
+    {% for extra in data.extras %}
+      {% if extra.key not in ['person','street','location','telephone','mail','web','gnd'] %}
+        {% set prefix = 'extras__%d__' % (loop.index0 + 7) %}
+        {{ form.custom(
+          names=(prefix ~ 'key', prefix ~ 'value', prefix ~ 'deleted'),
+          id='field-extras-%d' % loop.index,
+          label=_('Custom Field'),
+          values=(extra.key, extra.value, extra.deleted),
+          error=errors[prefix ~ 'key'] or errors[prefix ~ 'value']
+        ) }}
+        {% endif %}
+    {% endfor %}
+  
+    {# Add a max of 3 empty columns #}
+    {% set total_extras = data.extras|count %}
+    {% set empty_extras = (limit or 3) - total_extras %}
+    {% if empty_extras <= 0 %}{% set empty_extras = 1 %}{% endif %}
+  
+    {% for extra in range(total_extras, total_extras + empty_extras) %}
+      {% set index = loop.index0 + (data.extras|count) + 7 %}
+      {% set prefix = 'extras__%d__' % index %}
+      {{ form.custom(
+        names=(prefix ~ 'key', prefix ~ 'value', prefix ~ 'deleted'),
+        id='field-extras-%d' % index,
+        label=_('Custom Field'),
+        values=(extra.key, extra.value, extra.deleted),
+        error=errors[prefix ~ 'key'] or errors[prefix ~ 'value']
+      ) }}
+    {% endfor %}
+  </div>
+  {% endblock %}
 
   {{ form.required_message() }}
 
-- 
GitLab