Creating a custom administrator view of content

Benjamin Melançon's picture
Submitted by Benjamin Melançon on 2010, July 11 - 18:18
  • The generic goal is a view of content useful to administrators or other privileged users. If it were possible for this goal to be met in a generic fashion, it is more than likely Drupal (or a module) would be providing it for us already, out of the box. Indeed, Drupal core does provide a very useful list of content available at the Content or Find content links (admin/content). This list of titles of all content posted to the site is sorted by most recently updated first, but can be sorted by title, type of content, and status (published or not). It is quite nice, and certainly generic.

    Our specific goal is creating a view of chapters for authors to use to see who is working on each chapter and the short internal names for chapters. It should display the chapters in the same order in which they appear in the book.

    We go to Structure » Views (admin/structure/views) and click Add new view (admin/structure/views/add)

    We give it a View name of chapters.

    We give a View description (which, like the view name, is only seen by administrators) of Chapter listing with internal names and authors.. We give it a View tag of dgd7, simply so we can easily keep track of the views we added specifically for this site.

    Finally, we make the View type Node, because nodes are the form most content on a Drupal site takes, including our Chapter content. Even though it was originally created by the book module and we renamed it to Chapter, it is a type of node just like the Basic page and Article provided by the default installation profile. or Suggestion or anything else created through Structure » Content types (admin/structure/types).

    After pressing Save to submit the simple form that gathers the initial basic information for our view, we come to one of the most intimidating configuration pages in all of Drupal. The most important thing for us to remember, as we look at this page and the layers of settings contained within, is Don't Panic. (We should also always travel with a towel, but that's another matter entirely.)

    Let's start with something really safe. Something to remind us why we came here in the first place. Let's give the view a default title. This is set in the second block down in the first main column, immediately under the top block of View settings, in a block called Basic settings. There we can set the Title to Chapter list.

    Why is this a default title? Because its the title for the Default display. Every view can have multiple displays. In fact, every view must have more than one display for normal usage. The Default display does not do anything on its own, but every other display inherits its settings– or overrides them. Even if our next display has the exact same settings as the Default display (does not override any settings), we need to have a Page display if we want our view to be able to seen as a page, or a Block display to see our view in a block, and so on. We can have more than one Page display or other type of display that are each variations on the view's Default display.

    Filtering what we show in our view display

    Title taken care of, we move to a configuration setting where we can do a lot of damage: Filters.

    For our view of chapters, our needs here are simple: We want filters which will show us only published book chapters.

    We find the Filters block at the end of the last column and click on the plus sign (+) to the right of the Filters label, just before the up-down arrows, the only other icon there. (The plus sign has the tooltip title "Add", the arrows have the tooltip "Rearrange".) The filters configuration appears below the three columns of settings blocks.

    What we have for the Filters configuration is a huge list of things we can filter by. Fortunately, we can filter this list of filters. This is what the dropdown select list labeled Groups is for. In this case, and in many cases, one of the filters we want is node type (called content type in the rest of the Drupal interface), and another is whether or not the node is published. Both are in the node group. So instead of scrolling through the long list, we can select Node as the group, and this restricts the very long list to merely a rather long list. We scroll down to Node: Published and checkmark it and then to Node: Type and checkmark it.

    With our two filters checkmarked, Node: Published and Node: Type, we press the Add button. (This is something we will do a lot when building views.) Views module politely stacks our further configuration options, giving us each one at a time.

    First we get to configure the filter Node: Published, and can select Yes under Published. We do this because we only want to be shown published nodes, as is usually the case. Then we click Update. (Foreshadowing: When we aren't working on the default display, it won't be this simple.)

    Now views serves up our second filter, Node: Type, to configure. To show only book chapters in this view, we keep the default Operator of Is one of and Chapter for the Node type.

    If we were really on the ball, we would have noticed the lone Book filter option, Book: Depth, at the top of the list. (The list is alphabetical, so the Book group stays at the top until we enable a module that happens to provide a group that starts with A or another B.) We realize now that the node that represents the book itself, and holds all of the other Chapter nodes is itself a Chapter (book) node. We want to exclude it, and can do so by excluding book nodes with a depth of 1, which is the top level or container of a book.

    We go back to the Filters plus sign and click it to open up the Filters configuration again. We select Book: Depth and click Add. We configure the book depth filter to have an Operator that Is not equal to a Value of 1.

    Now that we have filtered our view's default display to show the content that we want, we have to tell it how to show it. Views is already yelling at us that Display "Defaults" uses fields but there are none defined for it or all are excluded. Obedient to the machine, we add fields by going to the bottom of the second column and clicking the plus sign (+) on the Fields block which is there.

    We clicked the add fields plus sign and now we're look down for the configuration options. We can see that many of the same things from the filter list are available to us as fields (or vice versa, depending on one's perspective). The use is very different, however. In filters all these elements let us

    Fields: field_authors
    Fields: field_internal
    Node: Title
    Node: Edit link
    Node: Updated date

    We are given more options than we necessarily want to deal with. Fortunately, field_authors can be left with the defaults (nothing selected), except for the replacement of the Label field_authors with something a little prettier, such as Authors.

    Likewise, we leave field_internal the same except for changing its label to Internal name.

    Edit link we leave just as it defaults, Title we leave all the defaults except that we checkmark Link this field to its node at the bottom, and Updated date we leave with its defaults.

    We click the rearrange arrows, and change the order in which the fields are displayed. We can drag and drop them into the order we choose. Item in the listing may look redundant (for instance, "Title Title") because Views provides both the system or machine name and the label we have chosen to give it within the display.

    Node: Title Title
    Fields: field_authors Authors
    Fields: field_internal Internal name*
    Node: Updated date Updated date
    Node: Edit link Edit link

    Finally, under Style settings (the second block from the bottom in the first column) we see Style: Unformatted. We click on the Unformatted to change the presentation of our Default display, or, as Views puts it when the configuration shows up below, "How should this view be styled." We choose Table, and Update.

    Sort criteria is second from the bottom in the third column. We click its plus sign (+), select the single sort criterion of Book: Weight, and click Add. We leave Sort ascending selected and click Update.

    We add the Page display, and we only need to make one change: Giving the display a path. Under Page settings we click the None link that follows Path: and give it a value of authors-chapter-list in the form that appears below for Page: The menu path or URL of this view. Always, we remember to click Update.

    Save our view, go to the page (authors-chapter-list, and we're done.

    Under Basic settings we modify the Use pager setting, which defaults to a stingy ten items (in the case of node views, nodes). We click on Paged, 10 items and change it in the configuration options below to Display all items.

    Distinct - yes.
    TOFIX:
    An AJAX HTTP error occurred.
    HTTP Result Code: 500
    Debugging information follows.
    Path: /admin/structure/views/ajax/preview/chapters
    StatusText: Service unavailable (with message)
    ResponseText: Exception: d7cx: Column-level distinct is not supported yet. in views_plugin_query_default->compile_fields() (line 900 of /var/local/drupal/dgd7/drupal/sites/all/modules/views/plugins/views_plugin_query_default.inc).

    [group by author etc. didn't do anything]
    See also: http://capellic.com/blog/cure-duplicate-nodes-in-a-view

    --
    The little block to the left of the first column is really important. It is here that we add displays.

    [@TODO - add screenshots for everything here when Views 3 has stabilized.]

  • Explanation

  • We need a Views tutorial, and this is the first view i needed.
  • Book element

  • How to