<?php
$translate = $this->plugin('translate');

// Prepare the resource template query.
$ids = isset($query['resource_template_id']) ? $query['resource_template_id'] : [];
if (!is_array($ids)) {
    $ids = [$ids];
}
$ids = array_filter($ids);
if (!$ids) {
    $ids = [null];
}
?>
<div id="resource-templates" class="field removable multi-value" role="group" aria-labelledby="by-resource-template-label">
    <div class="field-meta">
        <span id="by-resource-template-label" class="label"><?php echo $translate('Search by template'); ?></span>
        <?php echo $this->hyperlink('', '#', ['class' => 'expand', 'title' => $translate('Expand')]); ?>
        <div class="collapsible">
            <div class="field-description"><?php echo $translate('Searches for resources that use any of these templates.'); ?></div>
        </div>
        <button type="button" class="add-value o-icon-add button" aria-label="<?php echo $translate('Add new template'); ?>" title="<?php echo $translate('Add new template'); ?>"></button>
    </div>
    <div class="inputs">
        <?php foreach ($ids as $id): ?>
        <div class="value">
            <?php echo $this->resourceTemplateSelect([
                'name' => 'resource_template_id[]',
                'attributes' => [
                    'value' => $id,
                    'aria-labelledby' =>  'by-resource-template-label'
                ],
            ]); ?>
            <button type="button" class="o-icon-delete remove-value button" aria-label="<?php echo $translate('Remove value'); ?>" title="<?php echo $translate('Remove value'); ?>"></button>
        </div>
        <?php endforeach; ?>
    </div>
</div>
