passing cck field as an argument in views - drupal

I have content type called event, which has a cck field called event id. The idea is that once a user goes that a url with that id, only information relating to that event id is displayed. In my view, i tried creating a block view and passed the event id as an argument, as soon as i do that, the results which formerly displayed in the live preview disappears..Not quite sure of what i am doing wrong, or if i should be block view. Should this be a page view? Please help.

I believe that you should create a Page View and not a block view. You have to pass a argument. Is Event ID equals to NodeID? If yes then you should add an Argument of type Node:NID and then set "Provide default argument" equals to "Node ID from URL".
If the EventId is NOT the NID then you should set the Default Argument to PHP and give the following PHP :
$path = drupal_get_path_alias($_GET["q"]); //get the URL alias
$path = explode("/", $path); //break path into an array
if ($path[0] == "events" && $path[1] != "")
{
return $path[1];
}
The above code will take the argument from the URL (the URL should be like www.example.com/events/14555
The reason that I am using the drupal_get_path_alias is because you may have enabled the pathauto module. If not you can just give the following php
if (arg(0) == ‘events’ && arg(1) != ”) return arg(1);

Related

Magnolia get content from children page in freetext

I have a tree structure and want to use this as a category based faq.
I know there is a categorization module, but i want to use a file tree structure.
so i had the following:
faq
- first category
- first entry
- second entry
- second category
- first entry
...
each page has an dialog with an headline and a description. thats the content i want to get for each page.
so currently i get the current page, and loop through the childpages
[#local currentPage=cmsfn.page(content) /]
[#local pageChildren = cmsfn.children(currentPage, "mgnl:page")]
[#list pageChildren as page ]
{
"category": "${page.title}",
[#local pageContent = cmsfn.contentById(page)]
"headline": "${pageContent.headline!?json_string}",
"description": "${pageContent.description!?json_string}"
} [#if page?has_next],[/#if]
[/#list]
Now i get the category (the page title) for each entry. Thats fine. But headline and description are not filled.
I know, page is a node. page is the page and can access each page property (title, template).
I didn't find anything to get the content from a page via cmsfn. There are methods for contentById, contentByPath and also page() to get the page for a content.
But how i can get the content, from a page?
Update:
I made big step.
I get my pageContent from contentByPath, and use here the path of the page.
After this, i convert this to an JCR Node.
[#local pageContent = cmsfn.contentByPath(page.#path)]
[#local jcr = cmsfn.asJCRNode(pageContent)]
Now dumping shows my jcr.
Node (/my-website/faqs/kategorie1/question1)
footer = Node (/my-website/faqs/kategorie1/question1/footer)
mgnl:activationStatus = true (Boolean)
[...]
jcr:uuid = "a09d13da-3549-4b2a-8609-3b635e6f1c59" (String)
layers = Node (/my-website/faqs/kategorie1/question1/layers)
mgnl:activationStatus = true (Boolean)
[...]
jcr:uuid = "08b52c64-7327-4300-884c-047e42f560d0" (String)
overlays = Node (/my-website/faqs/kategorie1/question1/overlays)
mgnl:activationStatus = true (Boolean)
[...]
jcr:uuid = "9a78479b-0d25-4acf-9554-b9d626a7cc61" (String)
content = Node (/my-website/faqs/kategorie1/question1/content)
0 = Node (/my-website/faqs/kategorie1/question1/content/0)
description = "something" (String)
headline = "my example" (String)
mgnl:activationStatus = true (Boolean)
mgnl:created = Jul 24, 2019 11:20:40 AM UTC (Date)
What i really need, is here the content node, and the description and the headline.
But how to access this, and are there better methods to get this content?
You've already gotten the title of a page and any other property on the page node behaves just same as the title property. Hence, page.description will give you the description property of the given page node/object.
Let's also clarify what #contentById() method does. It is responsible to fetch a node by its identifier and returns a ContentMap e.g.
ContentMap (think as a Node object but mere Map) contentById(String identifierOfRequestedNode)
Update:
Via contentById method, you can directly access description and name, you do not need to convert it to the Node object.
E.g.
pageContent.description
pageContent.name
Hope that helps,
Cheers,

Adding users and attributes programatically in Concrete5 version 8

Hi I have recently followed some documentation to create new users from a csv file programatically. According to the Concrete5 docs/api there was method called getByID( $uID ) but this has since be deprecated!
I am creating a new user like so:
$userRegistration = Core::make('user/registration');
$file = fopen("test-users.csv","r");
while (($data = fgetcsv($file)) !== FALSE) {
echo "email address " . $data[0];
$userRegistration->create([
'uName' => $data[0],
'uPassword' => $data[1],
'uEmail' => $data[2],
'uIsValidated' => true,
]);
}
However if I want to add a value to an existing non-core attribute for instance lets call it user_county then how would I change this after programatically adding the user? I may need to do this for several user attributes as well so the values would need to come from the CSV and automatically be looped through to apply the correct value to the corresponding attribute whether it is blank or filled.
The create() method will return a UserInfo object (Concrete\Core\User\UserInfo) after successfully adding a new user. With the returned UserInfo object, you can use the setAttribute() method to set your custom user attribute.
Make sure that you have created the customer user attribute first and check that it is available before setting it, otherwise setting it will throw an error. I believe you can do this using Concrete\Core\Attribute\Key\UserKey::getByHandle('my_user_attribute') and seeing if it returns an object.
The create() method is in the RegistrationService class:
https://github.com/concrete5/concrete5/blob/develop/concrete/src/User/RegistrationService.php#L51-L140

Drupal 8 Image Field value

I'm trying to figure out how to get the path to an image from an entity in Drupal 8. I had thought get()->value would do it, but that just returns a blank string.
I have a test function:
function getValueTest ($profile_id, $field)
{
$profile_storage = \Drupal::entityManager()->getStorage('profile');
$profile = $profile_storage->load($profile_id);
if ($profile != null)
{
if ($profile->hasField ($field))
{
return $profile->get ($field)->value;
}
}
return "No field" . $field;
}
Assume some profile id 3 that has two fields field_first_name and field_mugshot. If I call:
dpm ($this->getValueTest (3, 'field_first_name'));
dpm ($this->getValueTest (3, 'field_mugshot'));
The first call correctly displays the first name in the message area, but the second just gives a blank string. I need a path to the image so I can do some processing on its content.
You can use the folowing methods to get the uri or the url:
$entity->get('field_image')->entity->getFileUri();
$entity->get('field_image')->entity->url();
This is because value() method should return the value of the field(aka a fid), field which is an entity reference in this case.

Keyword has incorrect values in custom meta

I have a category with keywords which in their tern have metadata schema. That schema consist of two fields and each of them is category. Very simple structure, but during publishing it resolves those metadata keyword fields into wrong tcm uris instead of title of the keyword, like the following:
2) Content of the deployer package
<tcmc:Topic rdf:about="tcm:10-11325-1024">
<rdfs:label>Analytics and optimization</rdfs:label>
<rdfs:comment>Analytics and optimization</rdfs:comment>
<tcmt:key>Analytics and optimization</tcmt:key>
<tcmt:isAbstract>false</tcmt:isAbstract>
<tcmt:isRoot>true</tcmt:isRoot>
<tcmt:metadata rdf:parseType="Literal">
<Metadata xmlns="uuid:a30b06d3-b6c5-4c2e-a53b-2b88771370ed">
<Divisions xlink:title="cma" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-17737-1024">cma</Divisions>
<InterestProfile xlink:title="CMAAnalytics" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="tcm:0-11175-1024">CMAAnalytics</InterestProfile>
</Metadata>
</tcmt:metadata>
</tcmc:Topic>
3) In code where I query Tridion it returns these uris:
TaxonomyFactory taxonomyFactory = new TaxonomyFactory();
TKeyword taxonomy = taxonomyFactory.GetTaxonomyKeywords(“tcm_of_the_category”);
if (taxonomy != null && taxonomy.KeywordChildren != null)
{
foreach (var item in taxonomy.KeywordChildren) //keyword metadata contains tcm uri with zero instead of title
{
Keyword keywordChildren = item as Keyword;
if (keywordChildren != null)
{
. . .
}
}
}
Does anyone have any ideas what might cause such an issue?
At a glance, my guess is that the internal template used to transform the categories is reading the metadata field data directly from the DB (or near enough in the BL layer) and not applying any blueprinting rules to it (likely for performance).
If you look at TCM Uris in content, when stored in the database, they all use 0 as their publication ID, and this ID is modified at "read" time.
Your call: You can call this a defect, ask Tridion to fix it, and it will degrade the performance of publishing a category, or you can deal with it in the delivery side - you know the publication Uri is 0, and you know you need to replace it with the current publication ID if you intend to use it for any purpose.
EDIT
So I went back and did some quick hacking. Indeed you can't load the keyword's content because, according to Tridion, the "Value" of field "Divisions" is the keyword URI. No way around that.
Quick way around this: load the keyword in question:
TaxonomyFactory tf = new TaxonomyFactory();
Keyword taxonomy = tf.GetTaxonomyKeywords("tcm:5-369-512");
if(taxonomy != null && taxonomy.KeywordChildren != null)
{
foreach (Keyword item in taxonomy.KeywordChildren)
{
NameValuePair key = (NameValuePair) item.KeywordMeta.NameValues["Field1"];
string correctUri = key.Value.ToString().Replace("tcm:0-", "tcm:5-");
Keyword theOtherKeyword = tf.GetTaxonomyKeyword(correctUri);
string title = theOtherKeyword.KeywordName;
}
}
Now... you probably want to be a bit smarter than me on that creative publication ID rewrite :)
You can see the field as a Component Link, you link to a specific Keyword item (object). Therefor you primarily get the URI, and I don't think that it resolves automatically to the Value property.
So the next step would be to obtain the Keyword object using the URI, and possibly construct the URI to include the right publication context.

how to detect in drupal node form if its edit or add form?

Is there a way to detect if the node form being viewed is the "edit" or "add new node" form?
Detect where? In a hook_alter? In a template? Somewhere else?
In general, the approach would be to get ahold of the $node object, and see if it's nid field is set. If it is, it's an edit.
Also you can make use of URL, if you don't want to load entire node object.
When it is new node addition, then in the URL arg(0) will be "node", arg(1) will be "add", arg(2) will be "content_type_name" whereas in the case of node viewing arg(0) will be node and arg(1) will be nid(i.e Numeric).
This is just an alternative way to detect.
check these answers from drupal.stackexchange.com
for example:
function mymodule_form_node_form_alter(&$form, &$form_state) {
$node = $form_state['node'];
if (!isset($node->nid) || isset($node->is_new)) {
// This is a new node.
}
else {
// This is not a new node.
}
}
or use the arg() function as previously has been indicated.
if ($node->is_new) {do_something_for_new_node();}

Resources