Skip to content

Commit

Permalink
feat: add form helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Feb 22, 2023
1 parent 1fc9319 commit fe2b2db
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion RockMigrations.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function getModuleInfo()
{
return [
'title' => 'RockMigrations',
'version' => '3.2.0',
'version' => '3.3.0',
'summary' => 'The Ultimate Automation and Deployment-Tool for ProcessWire',
'autoload' => 2,
'singular' => true,
Expand Down Expand Up @@ -259,6 +259,23 @@ public function addStyles($styles)
}
}

/**
* Set columnWidth for multiple fields of a form
*
* Usage:
* $rm->columnWidth($form, [
* 'foo' => 33,
* 'bar' => 33,
* 'baz' => 33,
* ]);
*/
public function columnWidth(InputfieldWrapper $form, array $fields)
{
foreach ($fields as $name => $width) {
if ($f = $form->get($name)) $f->columnWidth($width);
}
}

/**
* Make all pages having given template be created on top of the list
* @return void
Expand All @@ -272,6 +289,22 @@ public function createOnTop($tpl)
});
}

/**
* Hide fields of given form
*
* Usage:
* $rm->hideFormFields($form, ['foo', 'bar', 'baz']);
* $rm->hideFormFields($form, 'foo, bar, baz');
*/
public function hideFormFields(InputfieldWrapper $form, $fields)
{
if (is_string($fields)) $fields = array_map('trim', explode(",", $fields));
foreach ($fields as $field) {
$f = $form->get($field);
if ($f) $f->collapsed = Inputfield::collapsedHidden;
}
}

/**
* Remove non-breaking spaces in string
* @return string
Expand All @@ -281,6 +314,19 @@ public function regularSpaces($str)
return preg_replace('/\xc2\xa0/', ' ', $str);
}

/**
* Remove fields of given form
*
* Usage:
* $rm->removeFormFields($form, ['foo', 'bar', 'baz']);
* $rm->removeFormFields($form, 'foo, bar, baz');
*/
public function removeFormFields(InputfieldWrapper $form, $fields)
{
if (is_string($fields)) $fields = array_map('trim', explode(",", $fields));
foreach ($fields as $field) $form->remove($field);
}

/**
* Compile LESS file and save CSS version
*
Expand Down Expand Up @@ -408,6 +454,17 @@ public function setPageNameFromTitle($template)
return $this->setPageNameFromField($template, 'title');
}

public function sortFormFields(InputfieldWrapper $form, $fields)
{
$current = array_shift($fields);
$current = $form->get($current) ?: $form->children()->last();
foreach ($fields as $field) {
if (!$f = $form->get($field)) continue;
$form->insertAfter($f, $current);
$current = $f;
}
}

/**
* Wrap fields of a form into a fieldset
*
Expand Down

0 comments on commit fe2b2db

Please sign in to comment.