How to get the token value from meta tag in a outlet page - Drupal 7? - drupal-7

I need to display the meta tag values from the meta tag tokens.
My Code:
function MyModule_html_head_alter(&$head_elements) {
$head_elements['metatag_description']['#value'] = $outlet_description;
}
Actual Output:
[node:node_sections] - [node:field_lead_text] Testing
Expecting Output:
Food - Testing lead text Testing

You have to use token_replace function
$menu = menu_get_item();
if($menu['path'] == 'node/%') { // check if you are on node page
$node = $menu['page arguments'][0];
$head_elements['metatag_description']['#value'] = token_replace($outlet_description, array('node' => $node));
}

Related

ReactJs: How to replace html and string template with a component?

I want to manage the content of the page from a content editor where I am getting page content from the API.
Check this screenshot.
I used two different react modules for this react-html-parser and react-string-replace but it is still not working.
Here is my code.
let pageData = '';
pageData = ReactHtmlParser(page.content);
// replacing contact us form with a contact us form component
pageData = reactStringReplace(pageData, '{CONTACT_US_FORM}', (match, i) => (
<ContactUsForm />
));
return <div>{pageData}</div>;
react-html-parser -> It is used to parse HTML tags which are in string format into tree of elements.
react-string-replace -> It is used to replace a string into react a component.
Note: If I use react-html-parser or react-string-replace individually then it works fine but it does not work together.
Any suggestion?
Depends on the expected structure of page.content. If it contains HTML you are right in using react-html-parser, which has a replace option.
import parse from 'html-react-parser';
const macro = '{CONTACT_US_FORM}';
const replaceContactUsForm = (domhandlerNode) => {
if (domhandlerNode.type === 'text' && domhandlerNode.data.includes(macro))
return <ContactUsForm />;
};
// ...
const elements = parse(page.content, { replace: replaceContactUsForm });
return <div>{elements}</div>;
Additionally, If the string {CONTACT_US_FORM} is embedded in text you could use react-string-replace to keep the rest of the text intact:
const replaceContactUsForm = (domhandlerNode) => {
if (domhandlerNode.type === 'text' && domhandlerNode.data.includes(macro))
return <>{reactStringReplace(domhandlerNode.data, macro, () => (<ContactUsForm />))}</>;
};
If page.content does not contain HTML you do not need react-html-parser. But judging from your screenshot some markup is probably contained.

Can I remove parameter from URL generated by video playlist in Elementor

Elementor Pro has a new widget, video playlist. It appends a parameter to the URL, like so: http://aaronpenton.net/ampcreative/vip/about-vip/?playlist=f68425e&video=b8a9967
This is obviously terrible for SEO and UX. Is there a way to remove the ?playlist=f68425e&video=b8a9967 ?
My brother help me with the next script.
Put a "HTML Elementor Widget" with the following:
<script>
function getURLParameter(name) {
return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);
}
function hideURLParams() {
//Parameters to hide (ie ?playlist=value, ?video=value, etc)
var hide = ['playlist','video'];
for(var h in hide) {
if(getURLParameter(h)) {
history.replaceState(null, document.getElementsByTagName("title")[0].innerHTML, window.location.pathname);
}
}
}
window.onload = hideURLParams;
</script>
This should do the trick.
const url = 'http://aaronpenton.net/ampcreative/vip/about-vip/?playlist=f68425e&video=b8a9967'
url.replace(url.split('/')[6], '')
split, well.. splits the string into an array by the /
character.
At index 6 the array contains the ?playlist=f68425e&video=b8a9967 substring which can than be removed (i.e. replaced by the empty string) using replace.
A more general approach to removing the last part of the url might
be to use the array length instead of specifying the index:
const url = 'http://aaronpenton.net/ampcreative/vip/about-vip/?playlist=f68425e&video=b8a9967'
const urlArr = url.split('/')
url.replace(urlArr[urlArr.length - 1], '')
Update:
Another way to do this is using the URL API
const url = new URL('http://aaronpenton.net/ampcreative/vip/about-vip/?playlist=f68425e&video=b8a9967');
const result = url.origin + url.pathname
or in a function:
const removeParameter = u => {
const url = new URL(u);
return url.origin + url.pathname
}
You might have to check the specification for further details (browser support etc)

Select urls from Typo3 Database via Typoscript query

First my goal: I want to create a list with links to internal pages. This list has to be ordered by creation date (crdate in the DB)
To achieve this, I wrote the following code:
99 = CONTENT
99.table = pages
99.select {
pidInList = root,-1
selectFields = url
orderBy = crdate
}
Sadly, this returns nothing.
For debugging I played with the different properties.
With
99 = CONTENT
99.table = tt_content
99.select {
pidInList = 1
orderBy = crdate
}
I cloned my rootpage. I got all records from it.
So I know that all page records should be stored in the Pages table with the url.
What am I doing wrong here?
Additional information: Typo3 8.7, no the default backend elements are not working for me, yes I have to do it in typoscript
Thanks in advance for any suggestions.
The field url in the pages record does not hold the url from that page in general.
It is used only for pages of type external URL, so the internal link to this page can be forwarded to that url.
If you want a link list of all your pages you need to create a link to these pages:
99 = CONTENT
99 {
table = pages
select {
// your selection here
}
renderObj = TEXT
renderObj {
typolink {
parameter.field = uid
}
}
}
This will give you a list (if wrapping the renderObj with <li>|</li>) of complete links with page title as link text.
If you want only urls you can add:
typolink {
returnLast = url
}
Without wrapping it will be a long string without separation.
EDIT:
99 = CONTENT
99 {
table = pages
select {
pidInList = 69
orderBy = crdate desc
}
wrap = <ul>|</ul>
renderObj = TEXT
renderObj {
wrap = <li>|</li>
typolink {
parameter.field = uid
}
if.isFalse.cObject = CONTENT
if.isFalse.cObject {
table = pages
select {
// this 'uid' is from context of current pages record we have selected above
pidInList.field = uid
// this 'uid' is the field from the records we are selecting here
selectFields = uid
}
renderObj = TEXT
// this 'uid' is the selected field above
// a field in the pages record of the subquery to decide if there are child pages
renderObj.field = uid
# renderObj.wrap = |,
}
}
}

I want to add dropdown that comes from custom table in typo3 solr facets

I have indexed page correctly and solr is working, but I want to insert facets.
I have write below typoscript for adding contain of table tx_srcdataprovider_domain_model_tag, it added in index queue but not indexed properly.
How do I show all tag fields in dropdown in facets?
Please give me some example to add facets in typo3 solr.
Thanks in advance
tx_srcdataprovider_domain_model_tag = 1
tx_srcdataprovider_domain_model_tag {
table = tx_srcdataprovider_domain_model_tag
fields {
tag = tag
// the special SOLR_CONTENT content object cleans HTML and RTE fields
content = SOLR_CONTENT
content {
field = description
}
// the special SOLR_RELATION content object resolves relations
category_stringM = SOLR_RELATION
category_stringM {
localField = category
multiValue = 1
}
// build the URL through typolink, make sure to use returnLast = url
url = TEXT
url {
typolink.parameter = 1
typolink.additionalParams = &tx_srcdataprovider_domain_model_tag[item]={field:uid}
typolink.additionalParams.insertData = 1
typolink.returnLast = url
typolink.useCacheHash = 1
}
// the special SOLR_MULTIVALUE content object allows to index multivalue fields
keywords = SOLR_MULTIVALUE
keywords {
field = keywords
}
tag_stringS = tag
}

Populate Webform hidden field with title of referring node

Drupal 7
I'm having a similar problem to one that's been presented previously but so far I've not been able to make any of the suggestions work.
I have 'Product' pages of content type 'Software Products'. I want to place a link on the product pages pointing to a Webform 'Request Information' I want to populate a (hidden) field on the form with the product name which is also the title of the referring product page.
I have tried the following but this just results in the title of the form being shown - not the referring page.
<?php
/**
* Implementation of hook_form_alter().
*/
function AddNodeInfoToForm_form_alter(&$form, $form_state, $form_id) {
switch($form_id) {
case 'webform_client_form_10': // the id of the form
{$current_object = menu_get_object();
$product_title = $current_object->title;
$form['submitted']['product']['#default_value'] = $product_title; }
return $form;
}
}
I would appreciate any pointers - I'm new to Drupal
That's quite a messy way round of doing what you need to, you should just put the product nid in the URL as part of the query string in the link from your product page and then load it up from the webform.
In your node template/preprocess:
$webform_path = 'node/10'; // Or whatever the webform's nid is
$link = l('Request Information', $webform_path, array(
'query' => array(
'product_nid' => $product_node->nid
)
));
echo $link;
Then in your form alter:
function AddNodeInfoToForm_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'webform_client_form_10' && isset($_GET['product_nid']) && is_numeric($_GET['product_nid'])) {
$product_node = node_load($_GET['product_nid']);
if ($product_node) {
$product_title = $product_node->title;
$form['submitted']['product']['#default_value'] = $product_title;
}
}
}
Note that you don't return the form from the hook_form_alter function, the $form variable is passed in by reference so changes are stored that way.

Resources