How do I add a property select to a site's navigation form?

I am creating a module that allows pages to be added via the site navigation. I have set up a controller that inserts a navigation-link-form using getFormTemplate().

I want to add a select input to choose properties, but I can’t get it to work. I’m trying to add it this way (I used the code in the application/view/common/navigation-link-form/browse.phtml file as an example):

<?php

$properties = isset($data['properties']) && '' !== trim($data['properties']) ? $data['properties'] : null;

$propertiesElement = (new Omeka\Form\Element\PropertySelect("properties"))
    ->setLabel($this->translate('Select properties'))
    ->setAttribute('data-name', 'properties');
    ?>
<label><?php echo $translate('Properties'); ?><?php echo $this->formElement($propertiesElement); ?></label>

?>

and I get this error:

Fatal error :  Uncaught Error: Call to a member function search() on null in my_omeka_installation/application/src/Form/Element/AbstractVocabularyMemberSelect.php:64
Stack trace:
#0 my_omeka_installation/application/src/Form/Element/PropertySelect.php(26): Omeka\Form\Element\AbstractVocabularyMemberSelect-&gt;getValueOptions()
#1 my_omeka_installation/vendor/laminas/laminas-form/src/View/Helper/FormSelect.php(112): Omeka\Form\Element\PropertySelect-&gt;getValueOptions()
#2 my_omeka_installation/vendor/laminas/laminas-form/src/View/Helper/FormSelect.php(86): Laminas\Form\View\Helper\FormSelect-&gt;render()
#3 my_omeka_installation/vendor/laminas/laminas-form/src/View/Helper/FormElement.php(168): Laminas\Form\View\Helper\FormSelect-&gt;__invoke()
#4 my_omeka_installation/vendor/laminas/laminas-form/src/View/Helper/FormElement.php(193): Laminas\Form\View\Helper\FormElement-&gt;renderHelper()
#5 my_omeka_installation/vendor/laminas/laminas-form/src/View/Helper/FormElement.php(114): Laminas\Form\View\Helper\FormElement-&gt;renderType()
#6 my_omeka_installation/vendor/laminas/laminas-form/src/View/Helper/FormElement.php(91): Laminas\Form\View\Helper\FormElement-&gt;render()
#7 my_omeka_installation/vendor/laminas/laminas-view/src/Renderer/PhpRenderer.php(407): Laminas\Form\View\Helper\FormElement-&gt;__invoke()
#8 my_omeka_installation/modules/ImgVisu/view/common/navigation-link-form/form.phtml(73): Laminas\View\Renderer\PhpRenderer-&gt;__call()
#9 my_omeka_installation/vendor/laminas/laminas-view/src/Renderer/PhpRenderer.php(519): include('...')
#10 my_omeka_installation/vendor/laminas/laminas-view/src/View.php(194): Laminas\View\Renderer\PhpRenderer-&gt;render()
#11 my_omeka_installation/vendor/laminas/laminas-mvc/src/View/Http/DefaultRenderingStrategy.php(98): Laminas\View\View-&gt;render()
#12 my_omeka_installation/vendor/laminas/laminas-eventmanager/src/EventManager.php(319): Laminas\Mvc\View\Http\DefaultRenderingStrategy-&gt;render()
#13 my_omeka_installation/vendor/laminas/laminas-eventmanager/src/EventManager.php(171): Laminas\EventManager\EventManager-&gt;triggerListeners()
#14 my_omeka_installation/vendor/laminas/laminas-mvc/src/View/Http/DefaultRenderingStrategy.php(116): Laminas\EventManager\EventManager-&gt;triggerEvent()
#15 my_omeka_installation/vendor/laminas/laminas-eventmanager/src/EventManager.php(319): Laminas\Mvc\View\Http\DefaultRenderingStrategy-&gt;render()
#16 my_omeka_installation/vendor/laminas/laminas-eventmanager/src/EventManager.php(171): Laminas\EventManager\EventManager-&gt;triggerListeners()
#17 my_omeka_installation/vendor/laminas/laminas-mvc/src/Application.php(360): Laminas\EventManager\EventManager-&gt;triggerEvent()
#18 my_omeka_installation/vendor/laminas/laminas-mvc/src/Application.php(341): Laminas\Mvc\Application-&gt;completeRequest()
#19 my_omeka_installation/index.php(21): Laminas\Mvc\Application-&gt;run()
#20 {main}
  thrown in my_omeka_installation/application/src/Form/Element/AbstractVocabularyMemberSelect.php on line 64

I tried adding it with the view helper propertySelect:

<?php echo $this->propertySelect([
    'name' => 'properties',
    'attributes' => [
        'class' => 'chosen-select',
        'multiple' => true,
        'data-placeholder' => 'Select properties', // @translate
    ],
]); ?>

but the Jquery Chosen plugin does not activate on the select.

How can I get a select input for all properties with the Jquery Chosen plugin activated?

The first error is expected because the PropertySelect element doesn’t have the API manager to search for properties. This is because you’ve constructed the property directly. The propertySelect view helper injects the API manager automatically, which is why you’re seeing it work to a degree.

Try something like this:

<label>
    <?php echo $this->translate('Properties'); ?>
    <?php echo $this->propertySelect([
        'name' => 'properties',
        'attributes' => [
            'multiple' => true,
            'class' => 'chosen-select',
            'data-placeholder' => 'Select properties', // @translate
        ],
    ]); ?>
</label>
<script>$(".chosen-select").chosen();</script>

Note that we’re invoking Chosen in a script.

Thanks for your reply. That helped me a lot, and everything is working now. Here is the code that works for me:

<?php
$properties = isset($data['properties']) && '' !== $data['properties'] ? $data['properties'] : null;
?>
<label>
  <?php echo $this->translate('Properties'); ?>
  <div class="column">
  <?php echo $this->propertySelect([
    'name' => 'properties',
    'options' => [],
    'attributes' => [
      'value' => $properties,
      'data-name' => 'properties',
      'multiple' => true,
      'class' => 'chosen-select',
      'data-placeholder' => 'Select properties', // @translate
    ],
  ]); ?>
  </div>
</label>

<script>
var chosenOptions = {
  width: '100%',
  include_group_label_in_selected: true,
};
$(".chosen-select").chosen(chosenOptions);
</script>

<style>
.jstree-editlink-container { overflow: initial; }
.jstree-editlink-container .column {
  float: right;
  width: 70%;
  font-weight: normal;
}
</style>