1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 09:44:38 +02:00

Add processwire/processwire-requests#445 - Add usage fieldset/info on field edit page primary tab

This commit is contained in:
Ryan Cramer
2022-06-03 14:55:45 -04:00
parent f0d06cbf8a
commit 73a31ef2f9
3 changed files with 120 additions and 43 deletions

View File

@@ -85,7 +85,7 @@ class ProcessWire extends Wire {
* Version suffix string (when applicable) * Version suffix string (when applicable)
* *
*/ */
const versionSuffix = ''; const versionSuffix = 'dev';
/** /**
* Minimum required index.php version, represented by the PROCESSWIRE define * Minimum required index.php version, represented by the PROCESSWIRE define

View File

@@ -117,3 +117,7 @@ ins {
del { del {
color: red color: red
} }
span.barely-visible {
opacity: 0.2;
}

View File

@@ -29,8 +29,8 @@
* @method buildEditFormCustom(InputfieldForm $form) * @method buildEditFormCustom(InputfieldForm $form)
* @method InputfieldWrapper buildEditFormDelete() * @method InputfieldWrapper buildEditFormDelete()
* @method InputfieldWrapper buildEditFormBasics() * @method InputfieldWrapper buildEditFormBasics()
* @method InputfieldWrapper buildEditFormInfo() Deprecated, hook buildEditFormActions() instead. * @method InputfieldWrapper buildEditFormInfo($form) Deprecated, hook buildEditFormActions() instead.
* @method InputfieldWrapper buildEditFormActions() * @method InputfieldWrapper buildEditFormActions(InputfieldForm $form)
* @method InputfieldWrapper buildEditFormAdvanced() * @method InputfieldWrapper buildEditFormAdvanced()
* *
* @method void fieldAdded(Field $field) * @method void fieldAdded(Field $field)
@@ -1107,9 +1107,9 @@ class ProcessField extends Process implements ConfigurableModule {
} }
if(!$this->fieldgroup) { if(!$this->fieldgroup) {
if($this->hasHook('buildEditFormInfo()')) { if($this->hasHook('buildEditFormInfo()')) {
$actions = $this->buildEditFormInfo(); // for legacy hooks $actions = $this->buildEditFormInfo($form); // for legacy hooks
} else { } else {
$actions = $this->buildEditFormActions(); $actions = $this->buildEditFormActions($form);
} }
if(count($actions)) $form->add($actions); if(count($actions)) $form->add($actions);
$this->buildEditFormContext($form); $this->buildEditFormContext($form);
@@ -1692,89 +1692,162 @@ class ProcessField extends Process implements ConfigurableModule {
$field->collapsed = Inputfield::collapsedBlank; $field->collapsed = Inputfield::collapsedBlank;
$form->add($field); $form->add($field);
/** @var InputfieldMarkup $field */
$field = $this->modules->get('InputfieldMarkup');
$field->attr('name', '_usage_table');
$field->label = $this->_('Usage');
$field->icon = 'search';
$field->collapsed = Inputfield::collapsedYes;
$field->value = 'value';
$form->add($field);
return $form; return $form;
} }
protected function ___buildEditFormInfo() { // legacy name (left for hooks) protected function ___buildEditFormInfo($form) { // legacy name (left for hooks)
return $this->buildEditFormActions(); return $this->buildEditFormActions($form);
} }
/** /**
* Build the 'Info' field shown in the Field Edit form * Build the 'Info' field shown in the Field Edit form
* *
* @param InputfieldForm $form Argument added 3.0.202
* @return InputfieldWrapper * @return InputfieldWrapper
* *
*/ */
protected function ___buildEditFormActions() { protected function ___buildEditFormActions(InputfieldForm $form) {
/** @var InputfieldWrapper $form */ $config = $this->wire()->config;
$form = $this->wire(new InputfieldWrapper()); $modules = $this->wire()->modules;
$form->attr('class', 'WireTab'); $fields = $this->wire()->fields;
$form->attr('id', 'info'); $sanitizer = $this->wire()->sanitizer;
$form->attr('title', $this->_x('Actions', 'tab'));
/** @var Templates $allTemplates */ $allTemplates = $this->wire()->templates;
$allTemplates = $this->wire('templates');
$inTemplates = array(); $inTemplates = array();
$multi = $this->field->type instanceof FieldtypeMulti;
/** @var InputfieldWrapper $fieldset */
$fieldset = $this->wire(new InputfieldWrapper());
$fieldset->attr('class', 'WireTab');
$fieldset->attr('id', 'info');
$fieldset->attr('title', $this->_x('Actions', 'tab'));
foreach($allTemplates as $template) { foreach($allTemplates as $template) {
if($template->fieldgroup->hasField($this->field)) $inTemplates[$template->id] = $template; if($template->fieldgroup->hasField($this->field)) {
$inTemplates[$template->id] = $template;
}
} }
/** @var MarkupAdminDataTable $table For display in Basics > Usage */
$table = $modules->get('MarkupAdminDataTable');
$table->setEncodeEntities(false);
/** @var InputfieldCheckboxes $field */ /** @var InputfieldCheckboxes $field */
$field = $this->modules->get('InputfieldCheckboxes'); $field = $modules->get('InputfieldCheckboxes');
$field->attr('name', 'send_templates'); $field->attr('name', 'send_templates');
$field->label = $this->_('Add or remove field from templates'); $field->label = $this->_('Add or remove field from templates');
$field->collapsed = Inputfield::collapsedYes; $field->collapsed = Inputfield::collapsedYes;
if(count($inTemplates)) { if(count($inTemplates)) {
$field->description = $this->_('This field is in use on the checked templates below.') . ' '; $field->description = $this->_('This field is in use on the checked templates below.') . ' ';
} else { } else {
$field->description = $this->_('This field is not currently in use on any templates.') . ' '; $field->description = $this->_('This field is not currently in use on any templates.') . ' ';
} }
$multi = $this->field->type instanceof FieldtypeMulti;
$field->description .= $this->_('You may quickly add or remove this field from templates by checking or unchecking the relevant boxes and clicking save.'); // Description for template usage $field->description .= $this->_('You may quickly add or remove this field from templates by checking or unchecking the relevant boxes and clicking save.'); // Description for template usage
$field->notes .= $this->_('You will be asked to confirm additions and removals on the next screen after you save.'); $field->notes .= $this->_('You will be asked to confirm additions and removals on the next screen after you save.');
$field->table = true; $field->table = true;
$field->icon = 'check-square'; $field->icon = 'check-square';
$populatedPagesLabel = $this->_x('Populated pages', 'usage-table-th');
$rowsOfDataLabel = $this->_x('Rows of data', 'usage-table-th');
$field->thead = $field->thead =
$this->labels['name'] . '|' . $this->labels['name'] . '|' .
$this->labels['label'] . '|' . $this->labels['label'] . '|' .
$this->_x('Populated pages', 'usage-table-th') . $populatedPagesLabel .
($multi ? '|' . $this->_x('Rows of data', 'usage-table-th') : ''); ($multi ? '|' . $rowsOfDataLabel : '');
$numOmitted = 0; $numOmitted = 0;
foreach($allTemplates as $template) { foreach($allTemplates as $template) {
/** @var Template $template */
$label = $template->name; $label = $template->name;
$longLabel = str_replace('|', ' ', $template->getLabel()); $templateLabel = $template->getLabel();
$longLabel = str_replace('|', ' ', $templateLabel);
$numRows = ''; $numRows = '';
$numPages = ''; $numPages = '';
if(isset($inTemplates[$template->id])) { if(isset($inTemplates[$template->id])) {
$numPages = $this->wire('fields')->getNumPages($this->field, array('template' => $template)); // field uses this template
$numRows = $multi ? $this->wire('fields')->getNumRows($this->field, array('template' => $template)) : null; $numPages = $fields->getNumPages($this->field, array('template' => $template));
$longLabel = "**[$longLabel](../template/edit?id=$template->id)**"; $numRows = $multi ? $fields->getNumRows($this->field, array('template' => $template)) : null;
$label = "**$label**"; $editUrl = "../template/edit?id=$template->id";
} else if($template->flags & Template::flagSystem) {
if(!$this->wire('config')->advanced) { $icon = $template->getIcon();
$numOmitted++; $tableRowName = $sanitizer->entities($label);
continue; if($templateLabel != $label) {
$tableRowLabel = $sanitizer->entities($templateLabel);
} else {
$tableRowLabel = "<span class='barely-visible'>$label</span>";
} }
} if($icon) $tableRowName = wireIconMarkup($icon, 'fw') . ' ' . $tableRowName;
$row = array("<a href='$editUrl' target='_blank'>$tableRowName</a>", $tableRowLabel, $numPages);
if($multi) $row[] = $numRows;
$table->row($row);
$longLabel = "**[$longLabel]($editUrl)**";
$label = "**$label**";
} else if($template->flags & Template::flagSystem && !$config->advanced) {
// field does not use template and it is a system template
$numOmitted++;
continue;
} else {
// field does not use template
if($longLabel === $label) $longLabel = "[span.barely-visible] $longLabel [/span]";
}
$label = "$label|$longLabel|$numPages" . ($multi ? "|$numRows" : ""); $label = "$label|$longLabel|$numPages" . ($multi ? "|$numRows" : "");
$field->addOption($template->id, $label); $field->addOption($template->id, $label);
} }
$field->attr('value', array_keys($inTemplates)); $field->attr('value', array_keys($inTemplates));
$field->set('_valuePrevious', $field->attr('value')); $field->set('_valuePrevious', $field->attr('value'));
if($numOmitted) $field->notes .= ' ' . sprintf($this->_('%d system templates are not shown because advanced mode is off.'), $numOmitted); if($numOmitted) $field->notes .= ' ' . sprintf($this->_('%d system templates are not shown because advanced mode is off.'), $numOmitted);
$form->add($field); $fieldset->add($field);
$field = $this->wire('modules')->get('InputfieldHidden'); // populate usage table from Basics tab
$tableHeaderRow = array(
$this->_('Template'),
$this->_('Label'),
$populatedPagesLabel
);
if($multi) $tableHeaderRow[] = $rowsOfDataLabel;
$table->headerRow($tableHeaderRow);
$usage = $form->getChildByName('_usage_table');
if($usage) {
if(count($inTemplates)) {
$usage->value = $table->render();
$usage->detail = $this->_('See the Actions tab to add/remove from templates.');
} else {
$usage->value = '<p>' . $this->_('This field is not currently in use.') . '</p>';
}
}
/** @var InputfieldHidden $field */
$field = $modules->get('InputfieldHidden');
$field->attr('id+name', '_send_templates_changed'); $field->attr('id+name', '_send_templates_changed');
$field->attr('value', ''); $field->attr('value', '');
$form->add($field); $fieldset->add($field);
// -------------------------- // --------------------------
/** @var InputfieldText $field */ /** @var InputfieldText $field */
$field = $this->modules->get("InputfieldText"); $field = $modules->get("InputfieldText");
$field->attr('id+name', '_clone_field'); $field->attr('id+name', '_clone_field');
$field->attr('value', ''); $field->attr('value', '');
$field->label = $this->_('Duplicate/clone this field?'); $field->label = $this->_('Duplicate/clone this field?');
@@ -1782,22 +1855,22 @@ class ProcessField extends Process implements ConfigurableModule {
$field->description = $this->_('To clone this field, enter the name of the new field you wish to create.'); // Description for clone field $field->description = $this->_('To clone this field, enter the name of the new field you wish to create.'); // Description for clone field
$field->notes = $this->_('Note that you will be editing your cloned copy after submitting this form.'); $field->notes = $this->_('Note that you will be editing your cloned copy after submitting this form.');
$field->collapsed = Inputfield::collapsedYes; $field->collapsed = Inputfield::collapsedYes;
$form->append($field); $fieldset->append($field);
// -------------------------- // --------------------------
/** @var InputfieldCheckbox $field */ /** @var InputfieldCheckbox $field */
$field = $this->modules->get('InputfieldCheckbox'); $field = $modules->get('InputfieldCheckbox');
$field->attr('name', '_optimize'); $field->attr('name', '_optimize');
$field->label = $this->_('Check field data'); $field->label = $this->_('Check field data');
$field->icon = 'medkit'; $field->icon = 'medkit';
$field->description = $this->_('Check the field for unused data or other possible optimizations. If any issues are found, you will have the opportunity to correct them from the Alert tab after saving.'); $field->description = $this->_('Check the field for unused data or other possible optimizations. If any issues are found, you will have the opportunity to correct them from the Alert tab after saving.');
$field->collapsed = Inputfield::collapsedBlank; $field->collapsed = Inputfield::collapsedBlank;
$form->add($field); $fieldset->add($field);
$form->add($this->buildEditFormDelete()); $fieldset->add($this->buildEditFormDelete());
return $form; return $fieldset;
} }
/** /**