Introduction to Drupal 7 News section and home page block with views
-
Upload
kalin-chernev -
Category
Technology
-
view
10.913 -
download
0
description
Transcript of Introduction to Drupal 7 News section and home page block with views
Introduction to Drupal 7
Session 3.2
News section and home page block with Views
1 Course by Init Lab (http://initlab.org) Drupalist: Kalin Chernev
Agenda
• What is Views
• Common uses
• Administration overviews
• Case study – making dynamic news section and latest news block for home page
2 Course by Init Lab (http://initlab.org) Drupalist: Kalin Chernev
What is Views
• The most popular Drupal module enabling regular (and not-technical) users to make lists, tables and other outputs of database queries in a fun way.
• What we’re going to use Views for:
– Latest news section
– About us section with team members
– Latest news block for home page
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 3
Views common use cases
• From http://drupal.org/project/views: – You like the default front page view, but you find you want to sort it
differently. – You like the default taxonomy/term view, but you find you want to sort it
differently; for example, alphabetically. – You use /tracker, but you want to restrict it to posts of a certain type. – You like the idea of the 'article' module, but it doesn't display articles the way
you like. – You want a way to display a block with the 5 most recent posts of some
particular type. – You want to provide 'unread forum posts'. – You want a monthly archive similar to the typical Movable Type/Wordpress
archives that displays a link to the in the form of "Month, YYYY (X)" where X is the number of posts that month, and displays them in a block. The links lead to a simple list of posts for that month.
• Views can do a lot more than that, but those are some of the obvious uses of Views.
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 4
Views administration overview
• Administration > Structure > Views
• URL: admin/structure/views
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 5
Creating latest news section
• Prerequisites for making it happen:
– Specific content type which will populate the section, i.e. news content type have to available
– News content type will have at least these 3 fields
• News image – for the news thumbnail
• Original publish date
• Body field or any other text area field
– Views and Chaos tools modules installed
– Already populated content would a plus
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 6
Creating latest news section, step 1
• Administration > Structure > Views > Add new view
• URL: admin/structure/views/add
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 7
Creating latest news section, step 2
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 8
Working with the controls
• Adding fields, criteria, etc
– You can use the add button directly
• Rearranging fields
– Use the options arrow on the add button
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 9
Creating latest news section, step 3
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 10
Format settings explained
• Main format: table (easiest to use for beginners)
• Title, date and body are grouped with <br/> in the end
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 11
Fields settings explained
• We added the following fields: news image, node title, original publish date and the body of the node;
• The order of the fields is important;
• The news image takes the news image preset;
• The node title links to the node itself;
• The publish date is long format;
• The body field is trimmed version to 500 symbols;
• No field labels are needed for this case;
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 12
Filter and sort criteria explained
• 2 filter criteria are used for our case:
– Content: Published (Yes) which filters the content in such a way, that only nodes that are made available for public will be available
– Content: Type (=News) which filters the content displayed to be only of type News
• 1 sorting criteria is used for our case:
– Content: Post date (desc) which makes so that the latest content is placed on top
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 13
Page settings explained
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 14
• Path: this is the URL which the page receives • Menu: use if you want to place the page in a menu item, i.e. in the main menu • Access: rarely used, if you want to directly place some permissions on the section • Header: used to place text/view before the result view • Footer: used to place text/view after the result view • Pager: self-explanatory – used to make pagers for your views section • More link: used in views blocks mainly
Advanced settings explained
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 15
• Contextual filters: used when dynamic arguments are passed from the URL for node id or user id
• Relationships: used when you need data from a database table, which is not accessible in your current state
• No results behavior: place text/view for cases when your query does not give results
• Exposed form: used when your filter criteria are exposed
• Other – Machine name: important
when you have many views – Query settings: removing
duplicates happen from here – Caching: when you want to
make performance tunings – CSS class: helping themers – Theme: advanced theming
What would an advanced user do
• Name your view better: for example ‘News views page’ instead of just ‘Page’
• Give a better machine readable name, for example ‘news_views_page’ instead of ‘page‘
• You can also tweak the table fields a bit, this is optional
• To become and advanced user: play with the views settings as much as you can;
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 16
Your dynamic news section is ready
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 17
Compile your work so far
• Go to admin/structure/features/create
• Set a name, description and a version
• Select your view and image style news
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 18
Adding a second view block
• We want to add another view, which will be a block, again showing the latest news items with a title and some text, no image or date
• The block will be used for the front page
• We can reuse some logic from our first view
• We’ll learn how to override views configurations
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 19
Latest news views block
• Go back to your view: admin/structure/views/view/{your_view}/edit
• Click “Add” and include a new views Block • The blocks automatically takes most of your views page
configurations • Override the following:
– The display name of the view – Remove the fields you don’t need – Limit the number of items to display to 5 – Change the machine readable name – Give a block name in the block settings
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 20
Important: overriding values
• Click on the a field where the override will be
• Select the override from the drop-down
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 21
Deleting fields from views
• You can either click on the field itself and delete it (after overriding it)
• Or you can use the Arrange option
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 22
Tweaking the latest news block view
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 23
Your home page news block is ready
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 24
Your features is overriden
• We made changes on our News view, and the Features modules sees the changes
• Therefore, if we work with a repository, the repository has be updated with our latest changes version (adding the views block)
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 25
Recreate the feature and save it
• Change the version to be up in some way
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 26
Time for questions
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 27
Contacts
Kalin Chernev
– E: [email protected]
– T: twitter.com/kalinchernev
– U: http://shtrak.eu/kalata
Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 28