From 9748927bf16271c66114dc20fe293070ef3e86bf Mon Sep 17 00:00:00 2001
From: ronaldmulero <ronaldmulero@241652.no-reply.drupal.org>
Date: Thu, 4 Sep 2014 19:14:25 -0400
Subject: [PATCH] Issue #2333155 by ronaldmulero: Allow unset form elements.

---
 context_form_alteration.module          |    2 ++
 context_reaction_alter_form.context.inc |   21 ++++++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/context_form_alteration.module b/context_form_alteration.module
index 515b087..22b10c9 100644
--- a/context_form_alteration.module
+++ b/context_form_alteration.module
@@ -149,6 +149,7 @@ function theme_context_form_alteration_settings_table($variables) {
         drupal_render($form[$key]['value']),
         drupal_render($form[$key]['append']),
         drupal_render($form[$key]['i18n']),
+        drupal_render($form[$key]['unset']),
       ),
     );
   }
@@ -159,6 +160,7 @@ function theme_context_form_alteration_settings_table($variables) {
     t('Value'),
     t('Append'),
     t('Translate'),
+    t('Unset'),
   );
   return theme('table', array('header' => $headers, 'rows' => $rows));
 }
diff --git a/context_reaction_alter_form.context.inc b/context_reaction_alter_form.context.inc
index 0d95a52..8fe94bc 100644
--- a/context_reaction_alter_form.context.inc
+++ b/context_reaction_alter_form.context.inc
@@ -24,6 +24,7 @@ class context_reaction_alter_form extends context_reaction {
         <li><b>Value:</b> What to add, alter or append.</li>
         <li><b>Append:</b> Add to a currently existing value. The alteration will detect array or string data type.</li>
         <li><b>Translate:</b> If value is a string which should be registered for interface traslation, eg: button text, label, etc.</li>
+        <li><b>Unset:</b> Remove the field from the form.</li>
       </ul><br>',
       array('!api_link' => l(
         t('Form API Reference'),
@@ -40,6 +41,7 @@ class context_reaction_alter_form extends context_reaction {
     $values = $this->fetch_from_context($context);
     if (isset($values['elements'])) {
       foreach ($values['elements'] as $val) {
+        $val['unset'] = empty($val['unset']) ? FALSE : $val['unset'];
         $form['elements'][$delta] = $this->build_row(
           $delta,
           $val['form_id'],
@@ -47,7 +49,8 @@ class context_reaction_alter_form extends context_reaction {
           $val['attribute'],
           $val['value'],
           $val['append'],
-          $val['i18n']
+          $val['i18n'],
+          $val['unset']
         );
         $delta++;
       }
@@ -120,10 +123,11 @@ class context_reaction_alter_form extends context_reaction {
                   $opt['value'] = i18n_string($i18n_context, $opt['value'], array('sanitize' => FALSE));
                 }
                 $append = empty($opt['append']) ? FALSE : $opt['append'];
+                $unset = empty($opt['unset']) ? FALSE : $opt['unset'];
 
                 // Sanitize and set the value.
                 $opt['value'] = filter_xss_admin($opt['value']);
-                $this->craf_drupal_array_set_nested_value($form, $parents, $opt['value'], FALSE, $append);
+                $this->craf_drupal_array_set_nested_value($form, $parents, $opt['value'], FALSE, $append, $unset);
               }
             }
           }
@@ -135,7 +139,7 @@ class context_reaction_alter_form extends context_reaction {
   /**
    * Build a form row.
    */
-  protected function build_row($delta, $form_id = NULL, $parents = NULL, $attribute = NULL, $value = NULL, $append = FALSE, $i18n = FALSE) {
+  protected function build_row($delta, $form_id = NULL, $parents = NULL, $attribute = NULL, $value = NULL, $append = FALSE, $i18n = FALSE, $unset = FALSE) {
     $form['form_id'] = array(
       '#default_value' => $form_id,
       '#type' => 'textfield',
@@ -168,13 +172,17 @@ class context_reaction_alter_form extends context_reaction {
       '#default_value' => $i18n,
       '#type' => 'checkbox',
     );
+    $form['unset'] = array(
+      '#default_value' => $unset,
+      '#type' => 'checkbox',
+    );
     return $form;
   }
 
   /**
    * Override of drupal_array_set_nested_value(), allows append.
    */
-  protected function craf_drupal_array_set_nested_value(array &$array, array $parents, $value, $force = FALSE, $append = FALSE) {
+  protected function craf_drupal_array_set_nested_value(array &$array, array $parents, $value, $force = FALSE, $append = FALSE, $unset = FALSE) {
     $ref = &$array;
     foreach ($parents as $parent) {
       // PHP auto-creates container arrays and NULL entries without error if $ref
@@ -184,7 +192,10 @@ class context_reaction_alter_form extends context_reaction {
       }
       $ref = &$ref[$parent];
     }
-    if ($append) {
+    if ($unset) {
+      unset($ref[$value]);
+    }
+    else if ($append) {
       if (is_array($ref)) {
         $ref[] = $value;
         return;
-- 
1.7.10.2 (Apple Git-33)

