Views Examples: Using Arguments and Relationships April 19, 2010 - - PowerPoint PPT Presentation
Views Examples: Using Arguments and Relationships April 19, 2010 - - PowerPoint PPT Presentation
Views Examples: Using Arguments and Relationships April 19, 2010 Michelle Lauer Drupal.org: miche Twitter: @bymiche Blog: developedbymiche.com/downloads CP: commonplaces.com/resources CommonPlaces.com Development Co-development & Mentoring
Marketing
Marketing Strategy & Mentoring User Training & Documentation Traditional & Cloud Hosting Security Services
Support
Application Support Site Maintenance Training & Documentation Traditional & Cloud Hosting Security Services
CommonPlaces.com
Planning
Business Strategy Consulting Information Architecture Performance Optimization Usability & Interface Design Detailed Project Specifications
Development
Co-development & Mentoring Application Integration Modules & Upgrades Open Source Development Visual Design & Theming
CommonPlaces develops integrated
- pen source
solutions designed to educate, engage, and entertain.
2
Welcome
You are here because…
- Want to be able to filter content in different
ways (arguments)
- Want to be able to display other information
(relationships) I will show you…
- Systematic way to create ALL views in the GUI
- What are arguments and how to use them
- What are relationships and how to use them
- Why you should export your Views to code
3
Hang On!
- Lots of information – FAST!
- Put your pencil down and absorb
- Remember the process
- Remember the big picture
- Remember the use cases
- Download the slides
- Look up the details
4
Background Information
What is a “site” module?
- Site- wide customizations
- hook_form_alter
- hook_nodeapi
- hook_menu
- hook_block
- hook_token_list, hook_token_values
- hook_views_default_views
Module Developer’s Guide: http://drupal.org/developing/modules Core module hooks: http://api.drupal.org/api/group/hooks Contributed module hooks: http://drupalcontrib.org/drupal-6-modules
5
Best Practices
- Create your own CCK body field
- Preface the name of your CCK fields with
content type they are used in.
- Fields that are used in several content
types don’t need the ctype name
- Follow the same series of steps when you
create each view
- Create Views and Node paths so each part
is a valid page
- First filter = Published nodes
- Export your views to code
6
7
Example 1 A Glossary of Terms
Example: A Glossary of Terms
Our goal is to create a way to browse through the instances of the content type glossary_item. When viewing the Glossary page, we can show all the items, but most people would prefer smaller chunks of content, so we will also provide pages for each first letter. When you navigate to a “letter” page, you only see nodes whose title begins with that letter.
8
Example: A Glossary of Terms
9
Definition: Arguments
Arguments are parts of the URL of a view that can be used to reduce the result-set.
- mysite.com/taxonomy/term/89
Arguments in Views are akin to arguments in
- functions. You are passing a variable to be
used.
- function mysite_do_something($arg)
10
Example: A Glossary of Terms
CONTRIBUTED MODULES DOWNLOADED
- Views
- Views Exporter
- Views UI
- CCK
- Content
- Text
- Pathauto: automatically generates
path aliases
- Token: placeholders for dynamic text
CORE MODULES ENABLED
- Path: create custom urls for
nodes CUSTOM MODULE CREATED
- drupalconsf2010: site-
specific customizations
11
Example: A Glossary of Terms
APPROACH
- Create Content Type
- Write Token Code
- Configure Pathauto
- Create Content
- Create View
- Place Block
CREATE VIEW
- Filters
- Fields
- Sort Criteria
- Page 1
- Group By
- Page 2
- Arguments
- Block
- Arguments
12
Create Content Type: glossary_item
Remove default “Body” field.
admin/content/types/add
13
Create Content Type: glossary_item
Add CCK “Body” field.
admin/content/node-type/glossary-item/fields
14
Write Token: first_letter
hook_token_list
15
Write Token: first_letter
hook_token_values
16
Configure Pathauto
Use the token we just created.
admin/build/path/pathauto
17
Create Content: Glossary Item
Notice the path for Blue
admin/content/node
18
Create View: Glossary
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Glossary Item
2. Fields
- Node: Title
- Content: Body
3. Sort Criteria
- Node: Title asc
4. Page: Landing
- Path = glossary
- Group By = Node: Title (length of 1)
5. Page: By Letter
- Path = glossary/%
- Arguments = Glossary Mode
6. Block: Letters
- Arguments = Summary Sorted
19
Create View: Glossary
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Glossary Item
2. Fields
- Node: Title
- Content: Body
3. Sort Criteria
- Node: Title asc
4. Page: Landing
- Path = glossary
- Group By = Node: Title (length of 1)
5. Page: By Letter
- Path = glossary/%
- Arguments = Glossary Mode
6. Block: Letters
- Arguments = Summary Sorted
20
Create View: Glossary
- 1. Filters - Add
21
Create View: Glossary
- 1. Filters - Add
22
Create View: Glossary
- 1. Filters – Add – Node: Published
23
Create View: Glossary
- 1. Filters – Add – Node: Type
24
Create View: Glossary
- 1. Filters
25
Create View: Glossary
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Glossary Item
2. Fields
- Node: Title
- Content: Body
3. Sort Criteria
- Node: Title asc
4. Page: Landing
- Path = glossary
- Group By = Node: Title (length of 1)
5. Page: By Letter
- Path = glossary/%
- Arguments = Glossary Mode
6. Block: Letters
- Arguments = Summary Sorted
26
Create View: Glossary
- 2. Fields – Add
27
Create View: Glossary
- 2. Fields – Add – Node: Title
28
Create View: Glossary
- 2. Fields – Add – Node: Title
29
Create View: Glossary
- 2. Fields – Add – Content: Body
30
Create View: Glossary
- 2. Fields
31
Create View: Glossary
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Glossary Item
2. Fields
- Node: Title
- Content: Body
3. Sort Criteria
- Node: Title asc
4. Page: Landing
- Path = glossary
- Group By = Node: Title (length of 1)
5. Page: By Letter
- Path = glossary/%
- Arguments = Glossary Mode
6. Block: Letters
- Arguments = Summary Sorted
32
Create View: Glossary
- 3. Sort Criteria – Add
33
Create View: Glossary
- 3. Sort Criteria – Add – Node: Title
34
Create View: Glossary
- 3. Sort Criteria
35
Create View: Glossary
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Glossary Item
2. Fields
- Node: Title
- Content: Body
3. Sort Criteria
- Node: Title asc
4. Page: Landing
- Path = glossary
- Group By = Node: Title (length of 1)
5. Page: By Letter
- Path = glossary/%
- Arguments = Glossary Mode
6. Block: Letters
- Arguments = Summary Sorted
36
Create View: Glossary
- 4. Page Display – Add
37
Create View: Glossary
- 4. Page Display – Change Name “Page: Landing”
38
Create View: Glossary
- 4. Page Display – Add Path “glossary”
39
Create View: Glossary
- 4. Page Display – Override Fields
40
Create View: Glossary
- 4. Page Display – Add Field – Node: Title
41
Create View: Glossary
- 4. Page Display – Override Style Settings – Group By
42
Create View: Glossary
- 4. Page Display – Override Style Settings – Group By
43
Create View: Glossary
- 4. Page Display – Preview
44
Create View: Glossary
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Glossary Item
2. Fields
- Node: Title
- Content: Body
3. Sort Criteria
- Node: Title asc
4. Page: Landing
- Path = glossary
- Group By = Node: Title (length of 1)
5. Page: By Letter
- Path = glossary/%
- Arguments = Glossary Mode
6. Block: Letters
- Arguments = Summary Sorted
45
Create View: Glossary
- 5. Page Display – Add
46
Create View: Glossary
- 5. Page Display – Change Name “Page: By Letter”
47
Create View: Glossary
- 5. Page Display – Add Path “glossary/%”
48
Create View: Glossary
- 5. Page Display – Override Arguments
49
Create View: Glossary
- 5. Page Display – Add Arguments
50
Create View: Glossary
- 5. Page Display – Configure Argument – Node: Title
51
Create View: Glossary
- 5. Page Display – Configure Argument – Node: Title
52
Create View: Glossary
- 5. Page Display – Preview
53
Create View: Glossary
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Glossary Item
2. Fields
- Node: Title
- Content: Body
3. Sort Criteria
- Node: Title asc
4. Page: Landing
- Path = glossary
- Group By = Node: Title (length of 1)
5. Page: By Letter
- Path = glossary/%
- Arguments = Glossary Mode
6. Block: Letters
- Arguments = Summary Sorted
54
Create View: Glossary
- 6. Block Display – Add
55
Create View: Glossary
- 6. Block Display – Change Name “Block: Letters”
56
Create View: Glossary
- 6. Block Display – Override Arguments
57
Create View: Glossary
- 6. Block Display – Add Arguments
58
Create View: Glossary
- 6. Block Display – Configure Arguments – Node: Title
59
Create View: Glossary
- 6. Block Display – Configure Arguments – Node: Title
60
Create View: Glossary
- 6. Block Display – Preview
61
Create View: Glossary
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Glossary Item
2. Fields
- Node: Title
- Content: Body
3. Sort Criteria
- Node: Title asc
4. Page: Landing
- Path = glossary
- Group By = Node: Title (length of 1)
5. Page: By Letter
- Path = glossary/%
- Arguments = Glossary Mode
6. Block: Letters
- Arguments = Summary Sorted
62
View: Glossary
mysite.com/glossary
63
View: Glossary
mysite.com/glossary/b
64
View: Glossary
mysite.com/glossary/b/blue
65
Create View: Glossary
Arguments – What did we just do? Page: Landing
- Added a field of Node: Title
- Used only the first letter
- Exclude from display
- Display settings -> Group by Node: Title
- Path = glossary
Page: By Letter
- Added argument of Node: Title
- Used only the first letter
- Hide page if no argument
- Basic Validation (just text)
- Path = glossary/%
Block
- Added argument of Node: Title
- Used only the first letter
- Summary Sorted if no argument
- Basic Validation (just text)
66
67
Example 2 Side Content
Example: Side Content
Our goal is to show specific related content in the sidebar for specific nodes. The content editor should be able to create as much content as they want and assign the page it should appear on.
68
View: Side Content
Preview
69
Definition: Relationships
A relationship allows you to expand the
- riginal query to include additional objects.
~Matt Tucker, PingVision
Find a common item between 2 different
- bjects so you can display related
information. If you want to reduce the results and cannot find what you are looking for in “filters”, there is a good chance that it is a “relationship”.
70
Example: Side Content
CONTRIBUTED MODULES DOWNLOADED
- Views
- Views Exporter
- Views UI
- CCK
- Content
- Option Widgets
- Node Reference: allows nodes to
be related
- Text
- Node Relationships: “Create and
Reference” feature
- Modal Frame API: pops up the
node add
- jQuery UI (plus library)
- Back Reference: maintain 1-1
relationships between all shared instances of a nodereference field
CUSTOM MODULE CREATED
- drupalconsf2010: site-
specific customizations
71
Example: Side Content
APPROACH
- Create Content Type
- Create Content
- Create View
- Filters
- Fields
- Arguments
- Relationships
- Block
- Place Block
CREATE VIEW
- Filters
- Fields
- Arguments
- Relationships
- Block
72
Create Content Type: side_content
Remove default “Body” field.
admin/content/types/add
73
Create Content Type: side_content
Add CCK “Body” field.
admin/content/node-type/side-content/fields
74
Create Content Type: side_content
Add CCK Node Reference field.
admin/content/node-type/side-content/fields
75
Create Content Type: side_content
Add CCK Node Reference field.
admin/content/node-type/side-content/fields
76
Edit Content Type: page
Add CCK Back Reference field.
admin/content/node-type/page/fields
77
Edit Content Type: page
Add CCK Back Reference field.
admin/content/node-type/side-content/fields
78
Edit Content Type: page
Configure Node Relationships
admin/content/node-type/page/relationships/noderef
79
Create Content: page
Result of Node Relationships Configuration
node/add/page
80
Create Content: page
Result of Node Relationships Configuration
node/add/page
81
Create Content
Page & Side Content
admin/content/node
82
Create View: Side Content
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Side Content
2. Fields
- Node: Title
- Content: Body
3. Arguments
- Node ID of Page = sidecontent_parent
4. Relationships
- Node ID of Page = sidecontent_parent
5. Block: Show Children
83
Create View: Side Content
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Side Content
2. Fields
- Node: Title
- Content: Body
3. Arguments
- Node ID of Page = sidecontent_parent
4. Relationships
- Node ID of Page = sidecontent_parent
5. Block: Show Children
84
Create View: Side Content
- 1. Filters - Add
85
Create View: Side Content
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Side Content
2. Fields
- Node: Title
- Content: Body
3. Arguments
- Node ID of Page = sidecontent_parent
4. Relationships
- Node ID of Page = sidecontent_parent
5. Block: Show Children
86
Create View: Side Content
- 2. Fields – Add
87
Create View: Side Content
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Side Content
2. Fields
- Node: Title
- Content: Body
3. Arguments
- Node ID of Page = sidecontent_parent
4. Relationships
- Node ID of Page = sidecontent_parent
5. Block: Show Children
88
Create View: Side Content
- 3. Arguments – Add
89
Create View: Side Content
- 3. Arguments – Add
90
Create View: Side Content
- 3. Arguments – Add
91
Create View: Side Content
- 3. Arguments – Add
92
Create View: Side Content
Arguments - What did we just do? If we are displaying a block of content (related nodes) on a page, the only information we have available to us is node id of the page we are on. We created an argument that is the node id of the page we are viewing AND equal to the value in the node reference of the side content. Page nid argument = Side Content node reference field nid (field_sidecontent_parent)
93
Create View: Side Content
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Side Content
2. Fields
- Node: Title
- Content: Body
3. Arguments
- Node ID of Page = sidecontent_parent
4. Relationships
- Node ID of Page = sidecontent_parent
5. Block: Show Children
94
Create View: Side Content
- 4. Relationships – Add
95
Create View: Side Content
- 4. Relationships – Add
96
Create View: Side Content
- 4. Relationships – Add
97
Create View: Side Content
Relationships- What did we just do? On a page, only show content where the page nid = the node reference (parent) nid in the side_content Page nid argument = Side Content node reference field nid (field_sidecontent_parent)
98
Create View: Side Content
What is the difference between the argument and relationship we just created? Argument = We want to use information from the url and how we want to use it Relationship = Only show content where the information from the url matches Page nid argument = Side Content node reference field nid (field_sidecontent_parent)
99
Create View: Side Content
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Side Content
2. Fields
- Node: Title
- Content: Body
3. Arguments
- Node ID of Page = sidecontent_parent
4. Relationships
- Node ID of Page = sidecontent_parent
5. Block: Show Children
100
Create View: Side Content
- 5. Block Display – Add
101
Create View: Side Content
1. Filters
- Node: Published = Yes
- Node: Type = Is one of Side Content
2. Fields
- Node: Title
- Content: Body
3. Arguments
- Node ID of Page = sidecontent_parent
4. Relationships
- Node ID of Page = sidecontent_parent
5. Block: Show Children
102
View: Side Content
Preview
103
Export Views – Why?
- Increase site portability
- The view is in code, as opposed to in
the database, so you can easily move it anywhere, like from a development environment to production
- Increase error tracking, enable “undo”
- You can use source control on
anything in code
104
Export Views
admin/build/views 3 States of Views NORMAL Only in the database OVERRIDDEN Original in code, current in the database DEFAULT Only in code
105
Export Views
Exported View code for mymodule/views/side_content.inc
106
Export Views
Exported View code in mymodule/views/side_content.inc
<?php [paste export here] $views[$view->name] = $view;
107
Export Views
Exported View code in mymodule/mymodule.module
108
Export Views
admin/build/views/tools admin/build/views admin/build/views
http://developedbymiche.com/blog/2009/09/20/drupalcon-paris-2009-staging-drupal
- -slide 27
109
Additional Resources
- Views 1 Arguments Documentation:
http://drupal.org/node/54455
- Arguments & Relationships
Example:
http://drupal.org/node/289738
- Arguments Video:
http://gotdrupal.com/videos/drupal- views-arguments
- Relationships Tutorial:
http://drupaleasy.com/blogs/ultimike/2 009/07/using-viewsrelationships- arguments-attachments
- Relationships Video:
http://drewish.com/node/127
- Relationships Video:
http://gotdrupal.com/videos/drupal-views-relationships
110
Credits
Development:
- Mike Machnik – commonplaces.com
Images: Calvin & Hobbes by Bill Watterson
- Fast: http://www.greenideasblog.com/molars/archives/2006/07/no_open_flame_b.html
- Arguments: http://www.weavingnetworks.com/2009/05/walking-their-mile-their-
way.html
- Relationships: http://dailygumboot.ca/2009/11/build-relationships-build-community-
hugs-and-other-acts-of-kindness/
- Side Content: http://reedsolomon.blogspot.com/2007/10/calvin-hobbes.html
- Background Information, Export Views, Resources:
http://forum.mininova.org/lofiversion/index.php?t235033603.html
Other Images:
- Glossary: http://thehyperblogger.com/blog/blog-marketing-a-definition-or-two/
- Magnifying Glass: http://margotmystic.wordpress.com/2008/09/20/palmistry-use-a-
magnifying-glass-medical-stigmata-lines/
Content
- http://www.wikipedia.org
111
Questions?
Michelle Lauer Drupal.org: miche Twitter: @bymiche Blog: developedbymiche.com/downloads CP: commonplaces.com/resources Free stickers available!
112