Jump to content

How can i access a specific item from the $item array?


Recommended Posts

Hey Guys,

so I am trying to add an if-clause to one of the templates (cms → front → global → embedRecord) and used

print_r($item)

to output all available information. I'd like to access the specific database-name where the record is being pulled from and can see that within the array:

[recordPage:protected]
(
	[_data:protected] => Array
                (
                    [id] => 8
                    [name] => Fancy Databasename

Unfortunately i do not know how to access the information directly from the template.

I've tried with

$item[recordPage][_data][name]

but that threw an error. 

How can I access this information?

Thanks!

 

 

Link to comment
Share on other sites

1 minute ago, Daniel F said:

There's no need to overcomplicate things:)

$item->database()->_title should work fine in this case

THANK YOU VERY MUCH! This works.

Unfortunately I do not know why and how 😞

Where can i find a list with functions/methods for the objects? 

Link to comment
Share on other sites

$item in this case is an instance of \IPS\cms\Records, a Pages database record.

This class has a method called "database()" that returns the reference to the database the record is stored in.

From there you want the title, which is accessible via the magic "_title" property.

A database is a node model: https://invisioncommunity.com/developers/docs/fundamentals/nodes/working-with-node-models-r28/

 

A database record is a content item model: https://invisioncommunity.com/developers/docs/fundamentals/content-items/building-a-model-for-content-items-r78/

 

 

Link to comment
Share on other sites

17 hours ago, bfarber said:

Thanks for the link! I have to say this is unfortunately a little bit above my paygrade 😉

Is there a full list of all available methods and properties aka a full list of "variables" that can be used within templates? I recall that previous versions of the community had a full list of variables in a sidebar to the template editor.

As of now I still would not know that theres the magic "_title" property if you had not told me. 

I've also encountered a very strange behaviour with the return value of $item->database()->_title.

This if clause always returned false:

DB Name: {$item->database()->_title}
{{if $item->database()->_title=="Episodendatenbank"}}
	TRUE
{{else}}
	FALSE
{{endif}}

Output:

DB Name: Episodendatenbank

	FALSE

I've changed my template with the same logic but am using $item->database()->_id - this works. But why does it not work with the string of the db-name?

Link to comment
Share on other sites

The _title is a hash value, so the equal check with "Episodendatenbank" will fail! you should use always use the id for such checks!

    /**
     * Get database name
     *
     * @return string
     */
    public function get__title()
    {
        return \IPS\Member::loggedIn()->language()->addToStack('content_db_' . $this->id);
    }

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...