I've got the component which displays the hierarchical data. It's a recursive data structure - a hierarchical classifier. Each category in the classifier consists of child categories and so on.
Here's the playground for it:
Relay playground
I've got a component showing this classifier in a third party html form. It is used to pick a category.
At some point in time a user will pick a category from this recursive classifier and I will have to get the full path of data that led to the current level. I mean if a user picks a category at the level 3 of the hierarchy, I must give back these data:
[root cat id, root cat name],
[level 1 cat id, level 1 cat name],
[level 2 cat id, level 2 cat name]
So when I catch the event of picking inside the component at the level 2 how can I get the full path from the root category from Relay store (from local graph)?
This has a React solution rather than a Relay one: how to access the path to a given node in the tree? I would synthesize a path, and pass it into your component.
const path = [
// The elements of the passed-in path.
...this.props.path,
// A new path element using the details from this level.
[this.props.catId, this.props.catName],
];
<MyNode path={path} onClick={() => alert(path)} />
Click here for a working example.
Related
I have a simple social-networking like graph w/ users, friends, comments, likes etc. Users can "own" items, comment on "items", like "items". I am trying to write a cypher query that returns "items" along w/ extra information to display them in my stream.
I have tried using optional match and collect and stuff, but there is always some part of the result that doesn't work.
Specifically, for a given user(say user1), I want to return "items" that:
a specific user + his friends own
show number of likes,
also show number of comments,
Know if the item is already owned by me (so I can hide "own" button in the UI)
If the item is owned by friends, I want to show name, image of up to 2 friends (but not more than 2 friends if, say, 5 friends own that item)
You can copy-paste below to get the graph
// 3 users
CREATE (u1:USER{name:"USER1", image: "image1"})
CREATE (u2:USER{name:"USER2", image: "image2"})
CREATE (u3:USER{name:"USER3", image: "image3"})
//3 items
CREATE (i1:ITEM{name:"ITEM1"})
CREATE (i2:ITEM{name:"ITEM2"})
CREATE (i3:ITEM{name:"ITEM3"})
// OWNERSHIP ..
//user1 owns 2 items
CREATE (u1)-[:OWNS]->(i1)
CREATE (u1)-[:OWNS]->(i2)
// user2 owns i2 and i3
CREATE (u2)-[:OWNS]->(i2)
CREATE (u2)-[:OWNS]->(i3)
// user3 also owns i2 and i3 (so i2 is owned by u1, u2 and u3; and i3 is owned by u2 and u3)
CREATE (u3)-[:OWNS]->(i2)
CREATE (u3)-[:OWNS]->(i3)
// FRIENDSHIP..
// user1 is friend of both user2 and user3
CREATE (u1)-[:FRIEND_OF]->(u2)
CREATE (u1)-[:FRIEND_OF]->(u3)
// COMMENTS ..
//user1 has commented on all those items he owns
CREATE (u1i1:COMMENT{text:"user1 comment on item1"})
CREATE (u1)-[:COMMENTED]->(u1i1)-[:COMMENT_FOR]->(i1)
CREATE (u1i2:COMMENT{text:"user1 comment on item2"})
CREATE (u1)-[:COMMENTED]->(u1i2)-[:COMMENT_FOR]->(i2)
//user 2 has also commented on all those he owns
CREATE (u2i2:COMMENT{text:"user2 comment on item2"})
CREATE (u2)-[:COMMENTED]->(u2i2)-[:COMMENT_FOR]->(i2)
CREATE (u2i3:COMMENT{text:"user2 comment on item3"})
CREATE (u2)-[:COMMENTED]->(u2i3)-[:COMMENT_FOR]->(i3)
// LIKES ..
//user1 has liked user2's and user3's items
CREATE (u1)-[:LIKED]->(i2)
CREATE (u1)-[:LIKED]->(i3)
//user2 has liked user1's items
CREATE (u2)-[:LIKED]->(i1)
Let's build your query up step by step:
Specifically, for a given user(say user1), I want to return "items" that:
a specific user + his friends own
MATCH (u:USER {name: "USER1"})-[:FRIEND_OF*0..1]-(friend:USER)-[:OWNS]-(i:ITEM)
WITH u,i,
// Know if the item is already owned by me (so I can hide "own" button in the UI)
sum(size((u)-[:OWNS]->(i))) > 0 as user_owns,
// If the item is owned by friends, I want to show name, image of up to 2 friends
collect({name:friend.name, image:friend.image})[0..2] as friends
RETURN u,i, user_owns, friends
// show number of likes,
sum(size(()-[:LIKED]->(i))) as like,
// also show number of comments,
sum(size(()-[:COMMENT_FOR]->(i))) as comments
Actually because it is such a good question, I sat down and created a GraphGist documenting each step here.
Fairly easy. First you need to have a variable path length match from 0..1 on FRIEND_OF returning either yourself. Follow to all items being owned by those.
Use OPTIONAL MATCH for likes and comments since there might or might not exist any.
Since there are potentially multiple paths to a single item, you need to count the distinct likes and comments.
To check if you already own the item, check the endpoint of the variable path match from above if its name is yours.
For getting up to two images of the friends owning the item filter the list for your friends and return the image property. Last step is to slice the collection for the first two elements using subscript operator.
MATCH (:USER { name:'USER1' })-[:FRIEND_OF*0..1]->(me_or_friend)-[:OWNS]->(item)
OPTIONAL MATCH (item)<-[l:LIKED]-()
OPTIONAL MATCH (item)<-[c:COMMENT_FOR]-()
WITH item, count(distinct l) AS likes, count(distinct c) AS comments,
collect(DISTINCT me_or_friend) AS me_or_friends
RETURN item, likes, comments,
ANY (x IN me_or_friends WHERE x.name='USER1') AS i_already_own,
[x IN me_or_friends WHERE x.name<>'USER1' | x.image][0..2] as friendImages
Final comment:
On SO we appreciate if you show in your question what you've already tried yourself to solve the problem. Question like "solve that problem for me" are not that much welcome.
I'm building an artgallery using Page Manager and Panels in Drupal 7.
A piece of art in the gallery is a node type and each one is related to a period of time, which is also a node type.
When viewing a page displaying a period of time, I need a link that takes the user to all the pieces of art relating to that period of time.
So I have made a view, that uses the nodeid of a timeperiod as a contextual filter, and gets all the artwork nodes, that are related to that particular timeperiod node.
The question is, when on the page for the timeperiod, how do I grab the nodeid of that particular timeperiod and dynamically generate a link to a page where the current nodeid is passed as an argument? (So that the correct pieces of art are fetched with the view).
You can use arg(1) in a piece of custom code to get the current node id (but only if it is a node you're on). See http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/arg/7. For instance you could easily embed your view in a template of your choice or a within a theme function like this:
views_embed_view('name_of_your_view', 'display_name', arg(1));
where the third parameter is the argument, the node id of your current (timeperiod) node. See http://api.drupal.org/api/views/views.module/function/views_embed_view/7.
To just place a link on that timeperiod node to that page (generated by your view) you would probably use a preprocess function in conjunction with a template (print if set). The possibilities there are uncountable, so you might need to provide further informations, where to place the link. But basically it can be achieved by using `arg(1)'.
I am trying to do something like this:
Project: Name, Due Date
Milestone: Due Date, Project
Task: Milestone, Title, Priority...etc
Is it possible to create a project with milestones and tasks all in one form and create all the nodes at once?
in this case drupal work with this as 1 node... you can create one node with multiple form..
use CCK with field group to group your node forms... you can create
Group for milestone form
Group for Task form
ALL of this is regarded as one node.. but i don't think u can insert more than one node in the same form
I'm working on a site that is a database of thousands record albums and am attempting to create an "album of the day" block. My solution has been to create a nodequeue of specific records, create a view that passes the current day of the year as an argument, and then uses this value to call the nodequeue item with that same numbered position. I do this by providing a "Default Argument" as PHP code in the "Nodequeue: Position" argument setting. Here's the code I use:
$nodequeueTotalNodes = 120;
$dayOfTheYear = date("z");
$nodeQueuePosition = $dayOfTheYear % $nodequeueTotalNodes;
return $nodeQueuePosition;
The above code works to my satisfaction but my problem is I have to manually change the value of $nodequeueTotalNodes every time I add or remove an item from my nodequeue.
Is there a way to pull the total number of nodes from my queue to replace the "120" in my code above?
The nodequeue_nodes table holds all the nodes in your queues. Something like this should do the trick, where qid is the queue id:
$nodequeueTotalNodes = db_result(db_query('SELECT COUNT(nid) FROM {nodequeue_nodes} WHERE qid = %d', $qid));
If you're using a subqueue there's a column called sqid which you can use.
I am working on a Drupal setup and have run into a slight issue relating nodes together using Views.
Essentially what I want to be able to do, is while editing Node A, have a multiple select node reference field to indicate it is related to Nodes B, C, D. When I view the contents of Node A, it should display the appropriate view for each of those nodes directly on Node A.
I worked through This Tutorial on using arguments in views, but it seems to be doing things in reverse of what I want. This method requires me to set the relationships in Nodes B, C and D to point to Node A instead of having Node A point to Nodes B, C and D. Setting the relationship in 3 different places instead of 1 location seems counter-intuitive and will be a pain to manage when we start applying this method to a bunch of different nodes.
I selected the following options for the argument:
// this is my multiple-select NodeReference relationship field
Content: Linked Highlight Boxes (field_linked_nodes)
Title: [blank]
Breadcrumb: [blank]
Action to take if argument is not present: Provide default argument
Default Argument Type: Node ID from URL
Validator: <Basic Validation>
Action to take if argument does not validate: Hide View / Page not found (404)
Wildcard: all
Wildcard title: All
Allow multiple terms per argument: checked
Exclude the argument: not checked
Let me know if you need any more information or explanation. Any help will be greatly appreciated.
Your are going to want the Node Relationships module: http://drupal.org/project/noderelationships. This will give you the back relationships that you want.