2

I have overwritten K2 Frontend item submit

I made a copy of

components/com_k2/templates/default/itemform.php

to

templates/YOUR TEMPLATE/html/com_k2/SUBTEMPLATE NAME/itemform.php

Based on the instructions here https://stackoverflow.com/questions/8623310/joomla-k2-frontend-page-modification

but now, i need to override the category list displayed on this item form.

                            <tr>
                                <td class="adminK2LeftCol">
                                    <label><?php echo JText::_('K2_CATEGORY'); ?></label>
                                </td>
                                <td class="adminK2RightCol">
                                    <?php echo $this->lists['categories']; ?>
                                </td>
                            </tr>

The itemmenu is configured to allow users to create itens only in few categories. The form don't permit in the others. But i wish that the category list display only allowed categories instead of all categories of the site.

The html result is that, the unauthorized categorys have a disabled="disabled"

                                <td class="adminK2LeftCol">
                                    <label>Categoria</label>
                                </td>
                                <td class="adminK2RightCol">
                                    <select id="catid" name="catid">
<option value="0">- Selecionar categoria -</option>
<option style="color: rgb(128, 128, 128);" value="1" disabled="disabled">Notícias</option>
<option style="color: rgb(128, 128, 128);" value="2" disabled="disabled">- - - Estado</option>
<option style="color: rgb(128, 128, 128);" value="3" disabled="disabled">- - - Política</option>
<option style="color: rgb(128, 128, 128);" value="4" disabled="disabled">- - - Esporte</option>
<option style="color: rgb(128, 128, 128);" value="27" disabled="disabled">- - - Policia</option>
<option style="color: rgb(128, 128, 128);" value="28" disabled="disabled">- - - Municípios</option>
<option style="color: rgb(128, 128, 128);" value="29" disabled="disabled">- - - Cultura</option>
<option style="color: rgb(128, 128, 128);" value="41" disabled="disabled">- - - Nacional</option>
<option style="color: rgb(128, 128, 128);" value="5" disabled="disabled">Colunas</option>
<option style="color: rgb(128, 128, 128);" value="6" disabled="disabled">- - - Trocando em miúdos</option>
<option style="color: rgb(128, 128, 128);" value="15" disabled="disabled">- - - Espaço da Cidadania</option>
<option style="color: rgb(128, 128, 128);" value="37" disabled="disabled">- - - Karine Rosa</option>
<option style="color: rgb(128, 128, 128);" value="7" disabled="disabled">- - - Colunistas</option>
<option style="color: rgb(128, 128, 128);" value="8" disabled="disabled">- - - - - Planeta vivo</option>
<option style="color: rgb(128, 128, 128);" value="13" disabled="disabled">- - - - - Economia</option>
<option style="color: rgb(128, 128, 128);" value="14" disabled="disabled">- - - - - Gastronomia</option>
<option style="color: rgb(128, 128, 128);" value="17" disabled="disabled">- - - - - Atualize</option>
<option style="color: rgb(128, 128, 128);" value="18" disabled="disabled">- - - - - Cultura Dinâmica</option>
<option style="color: rgb(128, 128, 128);" value="19" disabled="disabled">- - - - - Espaço Acadêmico</option>
<option style="color: rgb(128, 128, 128);" value="22" disabled="disabled">- - - - - Nosso direito</option>
<option style="color: rgb(128, 128, 128);" value="23" disabled="disabled">- - - - - Quebra galho</option>
<option style="color: rgb(128, 128, 128);" value="24" disabled="disabled">- - - - - Tecnologia</option>
<option style="color: rgb(128, 128, 128);" value="25" disabled="disabled">- - - - - Moda [**Categoria não publicada**]</option>
<option value="10" selected="selected">Classificados</option>
<option value="30">- - - Imóveis</option>
<option value="11">- - - - - Venda</option>
<option value="12">- - - - - Compra</option>
<option value="31">- - - Automóveis</option>
<option value="32">- - - - - Compra</option>
<option value="33">- - - - - Venda</option>
<option style="color: rgb(128, 128, 128);" value="16" disabled="disabled">Jurídica</option>
<option style="color: rgb(128, 128, 128);" value="26" disabled="disabled">Atividade parlamentar</option>
<option style="color: rgb(128, 128, 128);" value="42" disabled="disabled">- - - noticias parlamentares</option>
<option style="color: rgb(128, 128, 128);" value="20" disabled="disabled">Informativo</option>
<option style="color: rgb(128, 128, 128);" value="21" disabled="disabled">- - - Colunas</option>
<option style="color: rgb(128, 128, 128);" value="36" disabled="disabled">- - - Sobre a Gazeta</option>
<option style="color: rgb(128, 128, 128);" value="34" disabled="disabled">Minuto a minuto</option>
<option style="color: rgb(128, 128, 128);" value="38" disabled="disabled">- - - ACOMPANHE A COBERTURA DO IMPEACHMENT</option>
<option style="color: rgb(128, 128, 128);" value="39" disabled="disabled">ESPECIAL</option>
<option style="color: rgb(128, 128, 128);" value="40" disabled="disabled">- - - ACOMPANHE A COBERTURA DO IMPEACHMENT</option>

UPDATE:

i try to hide these categories with CSS

.adminK2RightCol > select option:disabled {
    visibility: hidden;
}

This hide the name of category in the dropdown list, but keeps a blank option line

  • So unauthorized categories have the class disabled and you wish to hide these from the dropdown? – Lodder Apr 29 '16 at 13:00
  • @Lodder yes. i'm currently considering using css... – Guilherme Apr 29 '16 at 13:02
  • You can append the hidden attribute to the option using JS but this will only work in IE11+ I think. Otherwise try setting using height: 0px in your CSS. Else see this: stackoverflow.com/questions/9234830/… – Lodder Apr 29 '16 at 13:33
  • @Lodder hiding using css and js I can't prevent the disable/enable by user of these categories via browser – Guilherme Apr 29 '16 at 16:57
3

There may be a correct way deeper in the depths of K2 to not have the disabled categories in the list in the first pace, however, the quickest solution is to use regex to remove them.

<?php $catinput = $this->lists['categories']; 
echo preg_replace('#<option[^>]*\sdisabled\=[^>]*>.*?</option>#','',$catinput); ?>
  • exactly what i was looking for, thks – Guilherme Apr 29 '16 at 16:50
  • No problem, glad to help. – David Hayes Apr 29 '16 at 16:53
  • @Lodder these are two more of my flags that serve no valuable purpose to researchers and were denied. (if showing kindness was the mission, that mission is complete) Who is making these judgement calls? – mickmackusa Dec 6 '18 at 6:56
0

When working with valid HTML, it is generally more stable to use a DOM parser versus regex. (Although in this case, David's solution shouldn't easily break)

Demo

$html = /* your HTML string */ ;

libxml_use_internal_errors(true);
$dom=new DOMDocument; 
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
$options = $xpath->query('//option[@disabled]');
for ($i = $options->length; --$i >= 0;) {  // when removing nodes from DOM, work in reverse order for stability
  $option = $options->item($i);
  $trailing_whitespace = $option->nextSibling;
  $option->parentNode->removeChild($option);
  $trailing_whitespace->parentNode->removeChild($trailing_whitespace);  // optionally remove line returns after removed tags
}

echo $dom->saveHTML();  // display the new HTML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.