Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
DReffects2 Posted December 2, 2020 Posted December 2, 2020 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!
DReffects2 Posted December 2, 2020 Author Posted December 2, 2020 1 minute ago, Adriano Faria said: $item->id. $item->name. Thanks. I am unsure what you mean by id. This is not a listing template
Daniel F Posted December 2, 2020 Posted December 2, 2020 There's no need to overcomplicate things:) $item->database()->_title should work fine in this case
DReffects2 Posted December 2, 2020 Author Posted December 2, 2020 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?
bfarber Posted December 3, 2020 Posted December 3, 2020 $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/ DReffects2 1
DReffects2 Posted December 4, 2020 Author Posted December 4, 2020 17 hours ago, bfarber said: 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/ 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?
Daniel F Posted December 4, 2020 Posted December 4, 2020 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); } DReffects2 1
Recommended Posts