Use calc element to extract label only from link element

Status
Not open for further replies.
I need to be able to output only the "label" of a link element to a custom list. I do not need the "link." In the following example, I only need to output the text: The Hard Cider Buzz.

{"label":"The Hard Cider Buzz","link":"http:\/\/www.foodandwine.com\/articles\/the-hard-cider-buzz"}


I do not want this output to be hyperlinked. Rather, I need the output to appear as basic text. (The reason for this is that I have the list published in a module where the entire module is linked to a particular page. I do not want the items in the list to contain their own links as well..)

After searching the forum, I think I need to use json_decode in the calc element, but I could not find an example close enough to what I am after to use as a starting point.

The list and field: press___headline.

Any ideas would be appreciated. Thanks.

J 2.5.14
Fabrik 3.0.8
ciderweekny.com (press dropdown on home page has the hyperlinked link I am trying to replace with regular text).
 
Try ...

PHP:
$link = json_decode('{press___headline}');
if (is_object($link)) {
    return $link->label;
}
return '';

-- hugh
 
I have played around with the syntax, but I can not get the first part of this argument to return anything. Instead, the else (' ') is all that is returning.

The closest example I could find was at: http://stackoverflow.com/questions/5633993/json-decode-help. This follows similar logic. I tried this and variants of this:
$value = '{press___headline}'
$link = json_decode($value);
return $link->label;

But that did not work either.

I tried a global scope on the original suggested code by Hugh, but that did not work either.
 
I'm not sure if its a copy and paste error but you have a missing semi-colon at the end of your first line, so the correct syntax would be:

PHP:
$value = '{press___headline}';
$link = json_decode($value);
return $link->label;

Hughs' example should work, you could add some debugging as follows to see why its not returning anything

PHP:
$link = json_decode('{press___headline}');
if (is_object($link)) {
   echo "{press___headline} is an object - value = ";
    print_r($link);
    exit;
    return $link->label;
} else {
  echo "{press___headline} is not an object";
  exit;
}
return '';
 
Thanks, Rob.

I can not get Hugh's example to work.

I ran it with the debugging and received the following:

{"label":"10 Things You Didn\u2019t Know About Hard Apple Cider","link":"http:\/\/newyorkcorkreport.com\/10-things-you-didnt-know-about-hard-apple-cider\/"} is not an object

Could it be the apostrophe in "Didn't" that is causing the problem? If one record fails does that end the argument so that no records are shown?




Based on this post: http://stackoverflow.com/questions/12210084/json-decode-returns-string-type-instead-of-object, I tried the following modification, but that did not work either.

PHP:
$link = json_decode('{press___headline}',true);
if (is_object($link)) {
    return $link->label;
}
return '';
Still plugging away. Any ideas would be appreciated.
 
the true property will just return an array rather than an object, so I don't think that should have an effect

I tried this:

PHP:
$link = '{"label":"10 Things You Didn\u2019t Know About Hard Apple Cider","link":"http:\/\/newyorkcorkreport.com\/10-things-you-didnt-know-about-hard-apple-cider\/"}';
$link = json_decode($link);
print_r($link);exit;

and that outputted an object for me. :

Code:
stdClass Object ( [label] => 10 Things You Didn’t Know About Hard Apple Cider [link] => http://newyorkcorkreport.com/10-things-you-didnt-know-about-hard-apple-cider/ )

what version of PHP are you using - perhaps this is an issue?
 
perhaps try forcing the string to uft-8? :

Code:
$link = utf8_encode($link);
 
I tried this:

PHP:
$link = '{"label":"10 Things You Didn\u2019t Know About Hard Apple Cider","link":"http:\/\/newyorkcorkreport.com\/10-things-you-didnt-know-about-hard-apple-cider\/"}';
$link = json_decode($link);
{
    echo $link->label;
}

And it outputted this:

Code:
10 Things You Didn?t Know About Hard Apple Cider

But echo loads the results above the table. Using return put the value in each record; however, I can not get it to work with the placeholder: {press___headline} like this:

PHP:
$link = '{press___headline}';
$link = json_decode($link);
{
    return $link->label;
}

This returns a blank.
 
are you really really sure that the place holder {press___headline} returns the same string {.......}, and that its' not already using the formatted version of the link? ('<a href="foo">bar</a>')?

I'd try this to be sure ....

PHP:
$link = '{press___headline_raw}';
$link =json_decode($link);
{
    return $link->label;
}

or perhaps

PHP:
$link = strip_tags('{press___headline}');
 
Thanks, Rob.

press___headline is indeed using the formatted version of the link: ('<a href="foo">bar</a>'). My problem is that I want to drop the hyperlink and just return the link title as non linked text.

The two examples in post #11 return no value when tried in the calc element: press___headlinenolink.

For reference, here is the default_row.php:
PHP:
<a href="http://ciderweekny.com/press">
<div id="<?php echo $this->_row->id;?>" class="<?php echo $this->_row->class;?>">
<table>
<tr>
          <td>
    <div id="pressarticledate" <?php echo @$this->cellClass['press___articledate']; ?>>
        <?php echo @$this->_row->data->press___articledate;?>
    </div>
 
    </td>
    </tr>
    <tr>
          <td>
 
    <div id="pressheadlineoutlet" <?php echo @$this->cellClass['press___headlinenolink']; ?>>
        <?php echo @$this->_row->data->press___headlinenolink;?>
    </div>
 
        </td>
    </tr>
    <tr>
          <td>
 
    <div id="pressheadlineoutlet" <?php echo @$this->cellClass['press___outlet']; ?>>
        <?php echo @$this->_row->data->press___outlet;?>
    </div>
 
        </td>
    </tr>
    <tr>
          <td>
    <div id="pressarticleauthor" <?php echo @$this->cellClass['press___articleauthor']; ?>>
        By: <?php echo @$this->_row->data->press___articleauthor;?>
    </div>
 
    </td>
    </tr>
 
 
</table>
 
</div>
</a>

Note that the entire thing is wrapped in the only hyperlink that I want to be active in the view, meaning that anywhere one clicks, they will be brought to ciderweekny/press. This is working (except that press___headline links to its unique link).

press___headline is a link element
press___headlinenolink is a calc element

When I simply put return '{press___headline}'; OR return '{press___headline_raw}'; in the calculation field for the element press___headlinenolink then I get:
When I edit the default_row.php above, changing press___headlinenolink to press___headline, then the view returns the label portion of the value as a hyperlink. I want the label portion of the value without the hyperlink so that clicking anywhere in the module takes the user to ciderweekny/press.
 
I hope I did it right. I tried the following without success:

Code:
$link = strip_tags'{press___headline_raw}';
$link =json_decode($link);
{
    return $link->label;
}

I also tried this without success:

Code:
$link = '{press___headline_raw}';
$link =json_decode($link);
{
    return $link->label;
}

Both examples above returned a blank value.
 
Nope, Rob is saying that it looks like the placeholder isn't JSON, it's a formatted string.

So try that second example he gave, without trying to json_decode.

-- hugh
 
::sigh::

OK, can you give me the info I need to jump on you site and fix this?

I'll need to form name it's on (for filtering the element list with), the element name, and link to the front end URL so I can test.

And can you make sure your My Sites info is current so i can log in.

-- hugh
 
Hugh:

Sorry to induce so many heavy sighs and thanks so much for your help.

The list/form is: PressFrontPageDropDown
The element is: headlinenolink (press___headlinenolink)
The URL is: http://ciderweekny.com/ (The Fabrik list "PressFrontPageDropDown" loads in a module via drop down menu under: "PRESS Keep current on news, updates and dispatches..." This drop down menu is located under the big yellow banner on the right side of the page. The default_row.php file from custom template for this list is pasted above in post #12.)

I just tested the login in my sites, and it is working.
 
Did you try the original example I gave :) :
PHP:
$link = strip_tags('{press___headline}');

Please remember that the none raw version of the placeholder contains the string "<a href="link">text</a>";
where as the raw version is a PHP object
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top