Introduction to Drupal 7 News section and home page block with views

28
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

description

Views 3 module overview. Making dynamic news section with Drupal Views module.

Transcript of Introduction to Drupal 7 News section and home page block with views

Page 1: 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

Page 2: Introduction to Drupal 7  News section and home page block with views

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

Page 3: Introduction to Drupal 7  News section and home page block with views

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

Page 4: Introduction to Drupal 7  News section and home page block with views

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

Page 5: Introduction to Drupal 7  News section and home page block with views

Views administration overview

• Administration > Structure > Views

• URL: admin/structure/views

Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 5

Page 6: Introduction to Drupal 7  News section and home page block with views

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

Page 7: Introduction to Drupal 7  News section and home page block with views

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

Page 8: Introduction to Drupal 7  News section and home page block with views

Creating latest news section, step 2

Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 8

Page 9: Introduction to Drupal 7  News section and home page block with views

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

Page 10: Introduction to Drupal 7  News section and home page block with views

Creating latest news section, step 3

Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 10

Page 11: Introduction to Drupal 7  News section and home page block with views

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

Page 12: Introduction to Drupal 7  News section and home page block with views

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

Page 13: Introduction to Drupal 7  News section and home page block with views

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 14: Introduction to Drupal 7  News section and home page block with views

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

Page 15: Introduction to Drupal 7  News section and home page block with views

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

Page 16: Introduction to Drupal 7  News section and home page block with views

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

Page 17: Introduction to Drupal 7  News section and home page block with views

Your dynamic news section is ready

Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 17

Page 18: Introduction to Drupal 7  News section and home page block with views

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

Page 19: Introduction to Drupal 7  News section and home page block with views

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

Page 20: Introduction to Drupal 7  News section and home page block with views

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

Page 21: Introduction to Drupal 7  News section and home page block with views

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

Page 22: Introduction to Drupal 7  News section and home page block with views

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

Page 23: Introduction to Drupal 7  News section and home page block with views

Tweaking the latest news block view

Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 23

Page 24: Introduction to Drupal 7  News section and home page block with views

Your home page news block is ready

Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 24

Page 25: Introduction to Drupal 7  News section and home page block with views

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

Page 26: Introduction to Drupal 7  News section and home page block with views

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

Page 27: Introduction to Drupal 7  News section and home page block with views

Time for questions

Drupalist: Kalin Chernev Course by Init Lab (http://initlab.org) 27

Page 28: Introduction to Drupal 7  News section and home page block with views

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