欢迎各位兄弟 发布技术文章
这里的技术是共享的
First, grab the Menu Attributes module and enable it either via Drush or download and enable it via the modules admin page. Once you enable Menu Attributes, visit /admin/structure/menu/settings and be sure that the "Classes" attribute is enabled. Now, you'll see this as a text field for any given menu item when editing those.
Now, we need to add Font Awesome and for the sake of this tutorial, I'll use the CDN version which we can add to our theme using a preprocess function. As aways, unless you're building a custom module, add the preprocess function to your theme's template.php file or create one if you don't have one already. For my preprocess function, I can use hook_preprocess_html
with drupal_add_css
.
Be sure to replace MYTHEME with the actual machine_name
of your theme and clear cache. If all went well, you'll now have Font awesome available to use. Note, you can also download Font Awesome and self-host it as an alternative to the CDN version.
The next step is to add some icon classes to your menu items via the Menu Attributes classes field. To see what icons are available to you, refer to the Font Awesome "Cheat Sheet" on their site. I'll add a "home" icon next to my home link so I simply edit the home link and add this to the classes field typically located at/admin/structure/menu/manage/main-menu:
fa fa-home
Above, the first fa
instantiates Font Awesome and fa-home
selects your icon. This will need a bit of theming and I found a few caveats as well. Since the class is added to your menu's "a" link, you'll want to define your font for the menu link itself and for the icon which gets added via a CSS "before" class. So if your menu id is primary-nav
, you can do this:
One issue I ran in to was that Firefox had trouble rendering the icons from the CDN and it's a documented issue apparently. To solve this, you may need to define Font Awesome in your theme's CSS via the @font-face
attribute with an absolute path to the CDN -- that solved it for me. Beyond this, you can style and color as needed all the while using CSS. That's pretty much all you need to do so as you can see it makes it trivial for users to add their own icons to menus.