A
A
Anton Essential2014-07-25 19:22:20
1C-Bitrix
Anton Essential, 2014-07-25 19:22:20

How to pass css class to array?

Good afternoon, gentlemen, please tell me how to add a css class to the menu array.

<?
$aMenuLinks = Array(
  Array(
    "", 
    "/index.php/", 
    Array(), 
    Array(), 
    "" 
  ),
        Array(
    "Каталог", 
    "/catalog/", 
    Array(), 
    Array(), 
    "" 
  ),
  Array(
    "Доставка", 
    "/Доставка/", 
    Array(), 
    Array(), 
    "" 
  )
);
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maksim Zverev, 2014-07-26
@AntonEssential

Do you need a class for some particular menu item?
And there cannot be a standard template, all default templates from the delivery are in the bitrix/components/menu/templates/ folder.
Then you just need to copy the folder of the menu template you are interested in to the folder with the site template. And there is an even simpler way, we put a menu close in functionality, turn on the editing mode in the public part, hover over the menu whose template we want to copy, a block of options will appear (there will also be a button to edit menu items), help from the gear, click on the arrow, calling additional .menyushku and there we choose to copy the template. Name it something (files will be copied to this folder), select Copy to site template - Current. And that's all. If you do not touch other checkboxes, the template file will open for editing.
Where will something like:

<? if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true) die(); ?>
<div class="menu menu--header">
<? if (!empty($arResult)): ?>
  <ul class='nav__items nav__items--header'>
  <? $previousLevel = 0; foreach ($arResult as $arItem):?>
  <? if ($previousLevel && $arItem["DEPTH_LEVEL"] < $previousLevel): ?>
    <?= str_repeat("</ul></li>", ($previousLevel - $arItem["DEPTH_LEVEL"])); ?>
  <? endif ?><? if ($arItem["IS_PARENT"]): ?><? if ($arItem["DEPTH_LEVEL"] == 1): ?>
  <li class='dropdown nav__item  nav__item--main'><a href="<?= $arItem["LINK"] ?>" class="nav__link nav__link--main dropdown-toggle"><?= $arItem["TEXT"] ?></a>
  <ul class='dropdown-menu'>
  <? else: ?>
  <li<? if ($arItem["SELECTED"]): ?> class="dropdown active"<? endif ?>>
  <a href="<?= $arItem["LINK"] ?>" class="dropdown-toggle"><?= $arItem["TEXT"] ?></a>
  <ul class='dropdown-menu'>
  <? endif ?>
  <? else: ?>
    <? if ($arItem["DEPTH_LEVEL"] == 1): ?>
      <li class="nav__item nav__item--main"><a class="nav__link nav__link--main" href="<?= $arItem["LINK"] ?>"><?= $arItem["TEXT"] ?></a></li>
    <? else: ?>
      <li><a href="<?= $arItem["LINK"] ?>"><?= $arItem["TEXT"] ?></a></li>
    <?endif ?>
  <?endif ?>
  <? $previousLevel = $arItem["DEPTH_LEVEL"]; ?>
<? endforeach ?>
  <? if ($previousLevel > 1): //close last item tags?>
    <?= str_repeat("</ul></li>", ($previousLevel - 1)); ?>
  <? endif ?>
  </ul>
<? endif ?>
</div>

If you need to add the ability to assign your own class to each individual item in the menu file (let's say for icons), then you need to:
1. Specify the parameter passed to the template, for example:
<?
$aMenuLinks = Array(
  Array(
    "", 
    "/index.php/", 
    Array(), 
    Array("menu_item_class" => "test_class"), 
    "" 
  ),
        Array(
    "Каталог", 
    "/catalog/", 
    Array(), 
    Array(), 
    "" 
  ),
  Array(
    "Доставка", 
    "/Доставка/", 
    Array(), 
    Array(), 
    "" 
  )
);
?>

2. Display this parameter in the menu template:
UPD: Looked at your example below. Based on it, I would do like this:
In the template file:
<?foreach($arResult as $arItem):
  if($arParams["MAX_LEVEL"] == 1 && $arItem["DEPTH_LEVEL"] > 1) continue;?>
    <li>
      <a href="<?=$arItem["LINK"]?>" class="menu__item_link
      <?if($arItem["SELECTED"]):?>active-menu <?endif?>
      <?if(isset($arItem["PARAMS"]["menu_item_class"])):?><?=$arItem["PARAMS"]["menu_item_class"]?><?endif?>
      "><?=$arItem["TEXT"]?></a>
    </li>
<?endforeach?>

The menu_item_link class is just a class on which styles can be hung, so as not to fence constructions of the form:
Which is very bad.

A
Anton Essential, 2014-07-26
@AntonEssential

Here is the actual menu template:

<?if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();?>

<?if (!empty($arResult)):?>
<ul class="clear-fix">

<?
foreach($arResult as $arItem):
  if($arParams["MAX_LEVEL"] == 1 && $arItem["DEPTH_LEVEL"] > 1) 
    continue;
?>
  <?if($arItem["SELECTED"]):?>
    <li><a href="<?=$arItem["LINK"]?>" class="active-menu"><?=$arItem["TEXT"]?></a></li>
  <?else:?>
    <li><a href="<?=$arItem["LINK"]?>"><?=$arItem["TEXT"]?></a></li>
  <?endif?>
  
<?endforeach?>

</ul>
<?endif?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question