[Drupalcon 2012] Session notes: View Modes—the many faces of your content

Presenter: Tim Cosgrove, Phase II

View modes:

  • Help you theme faster
  • Write less PHP
  • Create easily reusable layouts
  • Named ways of displaying entities within different context
    • "Full" — the actual node page
    • "Photo Teaser" — photo with title
    • "Dated Teaser" — date on top, summary
    • "Subtitle Teaser" — photo with summary, title
  • View modes are in core
  • View modes are NOT in Views
  • They're built into all Drupal 7 entities
    • Nodes
    • Users

Bad themer habits:

  • Too many .tpl files/custom templates
  • Too many functions in template.php
  • You use Views to do theming.
  • In D6, we needed these things, but NO MORE! D7 brings us View Modes!

How to use View modes:

  • In Manage Display, "default" and "teaser" are built in by default
    • "Full" — how to display the full page
    • "RSS" — how to display RSS feeds
    • "Search Index"
    • "Search Results" — how to display nodes in search results
  • To activate said view modes, click on custom display settings and activate the new ones you want; otherwise, default and teaser is it.
  • If you go into the view mode to edit, you can deactivate extra fields, or add new fields
    • This happens in code
    • You don't need to do a new node--type.tpl.php for each type anymore
  • BUT, we need more custom view modes! How?
    • Entity View mode
      • Simple, lightweight
      • does what it says
      • con: keeps EVERYTHING in one variable (this means that Features might get stuck if you use it with this module)
    • Display Suite
      • Has a better way of exporting view modes
      • Extremely powerful way of dealing with this
      • Con: it does A LOT OF THINGS. If you want that, great, but if you just need to add a couple of View modes, it's a bit much
    • Doing it in code.
      • Just need a little bit of code
        • use hook_entity_info_alter( )
        • change $info[node] and create a new machine name and array with qualities
        • add it to a module and throw it on your site.
        • Now it should show up in your view mode display panel.
  • Exporting view modes
    • Throw them into Features when you export your content types; they'll come back in when you import the content types into a new site
  • Use cases:
    • Node reference: add a custom view mode to the User Content page
  • Why not use Views for this stuff?
    • it's messy and complicated
    • takes significant time
    • too many templates, customized for each View
  • View modes actually make using Views much easier!
  • Other ways of using View modes:
    • Customizing search results
    • Customizing search index: offer more information to search index than is generally available to the index.
    • Create custom blocks with related content at the end of an article.
      • Big win: you don't have to theme, as long as someone sets up the display for that content type's block with "subtitle teaser."
  • Changing view modes:
    • make one change and it populates across things
  • Issues
    • Consistent design is key to using view modes
      • Make use of repeating elements
      • Use grid systems
      • The more you can use reusable elements, the better.
        • Save time & money
        • Look for inconsistencies; those will add to the dev time.
    • Also doesn't work with tables
    • Won't work with outputting fields from multiple entities at once, e.g. multiple content types or nodes.
    • Theming issues
      • Consistency is good, but boring sometimes
      • You might have to give a bit in order to get a design that the designer will be happy with.
      • Sometimes you might have to make a separate template, or preprocess something.
      • The way we were forced to do things in 5 and 6 forced us to do all these extra things.
        • We don't need to do this by default anymore; we can save it for when we actually need it.
  • bit.ly/view-modes (http://www.treehouseagency.com/learn/view-modes)
    • Learn more about View modes
    • download the code to make a new View mode module
  • BEANs (custom block entities) BoF (Thursday 2:15; room 506)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <img> <h1> <h2> <h3> <h4>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.