InputfieldPageAutocomplete

Autocomplete page selection input

It connects a text input to the ProcessPageSearch ajax API and stores selected pages as an array of page IDs. It is most often used as a delegate inputfield for InputfieldPage Page reference fields, but can also be configured directly.

$f = $modules->get('InputfieldPageAutocomplete');
$f->name = 'related_articles';
$f->label = 'Related articles';
$f->parent_id = $pages->get('/articles/')->id;
$f->template_id = $templates->get('article')->id;
$f->searchFields = 'title body';
$form->add($f);

For shared Inputfield behavior, see Inputfield. For Page reference field configuration, see InputfieldPage.

Expand all      API reference

Properties
PropertyTypeDefaultDescription
parent_idint0Limit ajax results to this parent ID. With findPagesSelector, it becomes a has_parent root constraint.
template_idint0Limit ajax results and selected-page lookup to one template ID.
template_idsarray[]Limit ajax results to multiple template IDs.
labelFieldNamestring'title'Field used for result and selected-item labels.
labelFieldFormatstring''Format string for labels; overrides labelFieldName when populated.
searchFieldsstring'title'Space-separated fields searched by autocomplete.
operatorstring'%='Selector operator used for autocomplete matching.
findPagesSelectorstring''Additional selector used for ajax page search.
maxSelectedItemsint0Maximum selected pages. 0 means unlimited; 1 enables single-select mode.
useListbooltrueRender a sortable selected-items list below the search input.
allowAnyValueboolfalseAllow unmatched typed text to remain in the visible input.
disableCharsstring''Characters that prevent autocomplete from triggering.
useAndWordsboolfalseSearch each word separately, similar to all-words matching.
lang_idint0Force a language ID for ajax results.
allowUnpubnull|bool|intnullWhether ajax results may include unpublished pages. false adds a published-status constraint.
usePageEditstring''Optional edit-link mode: small, medium, large, tab, or blank.
hideDeletedboolfalseHide deleted selected items immediately instead of marking them with undo state.

InputfieldPage usually sets most page-selection properties for you. Set them directly only when using this inputfield outside that delegate context.

Selected Pages

getSelectedPages()

Return selected page IDs as a PageArray.

$selected = $f->getSelectedPages();
foreach($selected as $page) {
    echo $page->title;
}

The lookup accepts the field value as an array or pipe-separated string. A single template_id and parent_id are used as lookup restrictions when present. When multiple template_ids are configured, selected-page lookup loads by ID without a single-template restriction.

Ajax URL

getAjaxUrl()

Build and return the ProcessPageSearch ajax URL used by the JavaScript autocomplete.

$url = $f->getAjaxUrl();

The selector stored for ProcessPageSearch is built from:

  • parent_id
  • template_id or template_ids
  • findPagesSelector
  • allowUnpub

If no selector constraints are configured, the selector falls back to id>0. Unless the selector already contains limit=, the URL adds limit=50.

When labelFieldFormat is populated, getAjaxUrl() stores the format with ProcessPageSearch and adds a format_name query parameter. Otherwise it adds get=<labelFieldName>.

Rendering

render()

Render the complete autocomplete widget. Output includes:

  • A hidden input containing selected page IDs as comma-separated values.
  • A visible text input for autocomplete searching.
  • A selected-items list when useList is enabled.
echo $f->render();

Use the base field name for $f->name; render() appends [] to the hidden input name automatically.

$f->name = 'related_pages'; // renders name='related_pages[]'

Do not include [] yourself, or the rendered name becomes related_pages[][].

When maxSelectedItems is 1, the input switches to single-select mode and useList is forced to false.

renderList()

Render the selected-items list as an <ol>.

echo $f->renderList();

renderListItem($label, $value, $class = '', Page $page = null)

Render one selected item. Hook this method to customize selected-list markup.

$wire->addHookAfter('InputfieldPageAutocomplete::renderListItem', function(HookEvent $event) {
    $page = $event->arguments(3);
    if($page && $page->template == 'article') {
        $event->return = str_replace('</span>', ' <em>article</em></span>', $event->return);
    }
});

When usePageEdit is set and the current user can edit the page, selected-item labels link to the page editor.

Processing Input

processInput(WireInputData $input)

Normalize submitted hidden input values into an array of integer page IDs.

// Submitted value: ['123,456']
$form->processInput($input->post);
$ids = $f->val(); // [123, 456]

Blank submitted values become an empty array.

Configuration

getConfigInputfields()

Return module configuration fields for:

  • Autocomplete search operator.
  • Fields to query.
  • Click-to-edit mode.
  • Delete behavior.

install() / uninstall()

Install and uninstall register or remove this inputfield from InputfieldPage's available selection widgets.

Hooks

Hookable methods:

MethodPurpose
getSelectedPages()Return selected pages as a PageArray.
getAjaxUrl()Return the ajax search URL.
render()Render the autocomplete widget.
renderList()Render selected-items list markup.
renderListItem($label, $value, $class = '', Page $page = null)Render one selected item.
processInput(WireInputData $input)Process submitted values.
install()Register with InputfieldPage.
uninstall()Remove from InputfieldPage.
getConfigInputfields()Return config fields.
Notes
  • Access with $modules->get('InputfieldPageAutocomplete').
  • Implements InputfieldHasArrayValue; values are arrays of page IDs.
  • Implements InputfieldHasSortableValue; selected page IDs preserve sort order.
  • getAjaxUrl() requires the current user to be able to execute ProcessPageSearch, which is normally true in the Page editor context.
  • hideDeleted adds a wrapper class in renderReady() and a list class in renderList().
  • Source file: wire/modules/Inputfield/InputfieldPageAutocomplete/InputfieldPageAutocomplete.module.

API reference: methods, properties, hooks

Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the InputfieldPageAutocomplete class also inherits all the methods and properties of: Inputfield, WireData and Wire.

Show class?     Show args?       Only hookable?    

Properties

NameReturnSummary 
InputfieldPageAutocomplete::allowAnyValue bool Allow any value to stay in the input, even if not selectable?
DEFAULT: false
 
InputfieldPageAutocomplete::allowUnpub null bool int Allow unpublished pages in results? (null=not set, 0|false=no, 1|true=yes)
DEFAULT: null
 
InputfieldPageAutocomplete::disableChars string Autocomplete won't be triggered if input contains any of the characters in this string.
DEFAULT: blank
 
InputfieldPageAutocomplete::findPagesSelector string Optional selector to use for finding pages
DEFAULT: blank
 
InputfieldPageAutocomplete::hideDeleted bool Hide items when deleted. If false, items remain but are marked for deletion 3.0.258+
DEFAULT: false
 
InputfieldPageAutocomplete::labelFieldFormat string Format string to display in the results, overrides labelFieldName when used .
DEFAULT: blank
 
InputfieldPageAutocomplete::labelFieldName string Field to display in the results.
DEFAULT: title
 
InputfieldPageAutocomplete::lang_id int Force use of this language ID for results
DEFAULT: 0
 
InputfieldPageAutocomplete::maxSelectedItems int Maximum number of items that may be selected (0=unlimited) .
DEFAULT: 0
 
InputfieldPageAutocomplete::operator string Selector operator to use in performing the search
DEFAULT: %=
 
InputfieldPageAutocomplete::parent_id int Limit results to this parent ID, or if combined with findPagesSelector, the search is performed as $pages->get($parent_id)->find() rather than $pages->find().
DEFAULT: 0
 
InputfieldPageAutocomplete::searchFields string Field(s) to search for text. Separate multiple by a space.
DEFAULT: title
 
InputfieldPageAutocomplete::template_id int Limit results to pages using this template ID.
DEFAULT: 0
 
InputfieldPageAutocomplete::template_ids array Limit results to pages using these template IDs (alternate to the single template_id).
DEFAULT: []
 
InputfieldPageAutocomplete::useAndWords bool When true, each word will be isolated to a separate searchFields=searchValue, which enables it to duplicate ~= operator behavior while using a %= operator .
DEFAULT: false
 
InputfieldPageAutocomplete::useList bool Whether or not to use a separate selected list. If false specified, selected item will be populated directly to the input.
DEFAULT: true
 
InputfieldPageAutocomplete::usePageEdit string Use page editor links for selected Page items, when user has edit permission? Use 'small', 'medium', 'large', 'tab', or '' to disable. 3.0.258+
DEFAULT: ''
 

Additional methods and properties

In addition to the methods and properties above, InputfieldPageAutocomplete also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.269