Joomla4 plugin getBody - plugins

In Joomla 3.x versions I use JResponse to get and set the body.
function onAfterRender() {
$content = JResponse::getBody();
...
}
But in Joomla4 it returns Class 'JResponse' not found.
So how to get the body in Joomla4?
Thanks

My cross-posted answer...
According to the documentation, JApplicationWeb seems to be alive and well in Joomla4.
since 2.5.0
note As of 4.0 this class will be abstract
The getters:
JApplicationWeb::getBody()
Or
JFactory::getApplication()->getBody()
The setters:
JApplicationWeb::setBody()
Or
JFactory::getApplication()->setBody()
Relevant references and examples:
https://forum.joomla.org/viewtopic.php?t=941835
https://joomla.stackexchange.com/q/20879/12352
https://joomla.stackexchange.com/q/10232/12352
https://joomla.stackexchange.com/q/9953/12352
https://joomla.stackexchange.com/q/23835/12352
https://joomla.stackexchange.com/q/9720/12352

In Joomla4 use app
function onAfterRender() {
$content = JFactory::getApplication()->getBody();
JFactory::getApplication()->setBody($body);
...
}

Related

#PathParam: No value being passed

I'm building a REST api using Quarkus and Kotlin. I'm trying to include a path parameter in my function by using the #PathParam annotation. This is what I have:
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("/{userId}")
fun getUser(#PathParam userId: UUID) : GetUserResponse =
try {
GetUserSuccess(userRepository.find("id", userId))
} catch (e: NotFoundException) {
GetUserFailure(e)
}
Unfortunately I'm getting an error stating that there's no value being passed for parameter value.
I googled some stuff, and most of what I found is about wrong imports. I double checked that part, but I import the correct one: import javax.ws.rs.*, which also includes the PathParam.
Anyone knows what's wrong with this?
The answer would be to change it to:
fun getUser(#PathParam("userId") userId : UUID)
Inspirerd by Paul Samsotha's answer.
Alternatively you could also use the #RestPath annotation from RESTEasy:
fun getUser(#RestPath userId: UUID)

SugarCRM: how to use preDisplay function in ViewQuickcreate?

I'm trying to customize the quick create view to add a default value of a field in Sugar Community Edition 6.5.24
Similar code works fine for ViewEdit, but it seems never called in subpanels.
Current file is
custom/modules/Opportunities/views/view.quickcreate.php
Unfortunately the constructor is not invoked.
Any help very appreciated.
<?php
require_once('include/MVC/View/views/view.quickcreate.php');
class OpportunitiesViewQuickcreate extends ViewQuickcreate {
function OpportunitiesViewQuickcreate(){
parent::ViewQuickcreate();
}
function preDisplay() {
parent::preDisplay();
$_REQUEST['custom_field_c'] = "a value for this field";
}
}
After tens of trying, I've found solution.
The right way is to extend SubpanelQuickCreate in the file custom/modules/Opportunities/views/view.subpanelquickcreate
require_once('include/EditView/SubpanelQuickCreate.php');
class OpportunitiesSubpanelQuickcreate extends SubpanelQuickCreate {
function OpportunitiesSubpanelQuickcreate() {
$_REQUEST['custom_field_c'] = "a value for this field";
parent::SubpanelQuickCreate("Opportunities");
}
}
Going from memory, so I may be wrong, but try adding $this->useForSubpanel = true; in your constructor.

In Scaladoc, how to link to a method in the same class?

What is the correct way to create link to a method in the same class?
Scaladoc documentation doesn't have examples of this.
Consider a class such as:
package controllers
// import ...
class AccountController extends Controller with Secured {
def current = Authenticated() { request =>
// ...
}
/**
* See [[current]] for an endpoint that...
*/
def findAll = Authenticated() { request =>
// ...
}
}
In IntelliJ IDEA's "quick documentation" pop up, all these show up as red:
[[current]]
[[#current]]
[[AccountController.current]]
[[AccountController#current]]
[[controllers.AccountController.current]]
This does show up as blue:
[[controllers.AccountController#current]]
So I suppose this is correct, but is there there no simpler yet correct way?
The correct way for your example would be:
[[controllers.AccountController#current()]]
The empty parenthesis are necessary here for functions without parameters, despite you shouldn't add these in the code. You also have to use fully-qualified names everywhere, this means it should contain the package, class name, a hashtag and the method name.
I am not sure when was it fixed, but I am using IntelliJ IDEA version 2020.3, and it works for me:
/**
* See [[current]] for an endpoint that...
*/

How to use Zend Framework's Partial Loop with Objects

I am quite confused how to use partialLoop
Currently I use
foreach ($childrenTodos as $childTodo) {
echo $this->partial('todos/_row.phtml', array('todo' => $childTodo));
}
$childrenTodos is a Doctrine\ORM\PersistantCollection, $childTodo is a Application\Models\Todo
I tried doing
echo $this->partialLoop('todos/_row.phtml', $childrenTodos)
->setObjectKey('Application\Models\Todo');
But in the partial when I try to access properties/functions of my Todo class, I cant seem to get them always ending up with either call to undefined method Zend_View::myFunction() when I use $this->myFunction() in the partial or if I try $this->todo->getName() I get "Call to a member function getName() on a non-object". How do I use partialLoops?
Try this
echo $this->partialLoop('todos/_row.phtml', $childrenTodos)
->setObjectKey('object');
Then in your partial you can access the object like this
$this->object
object is the name of the variable that an object will be assigned to
You can also do this once in your Bootstrap or other initialization class if you have access to the view object like so
protected function initPartialLoopObject()
{
$this->_view->partialLoop()->setObjectKey('object');
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->setView($this->_view);
}
I also had "Call to function on non object" error when trying suggested syntax, seems like they've changed something on later versions of Zend Framework. The following works for me on ZF1.12:
echo $this->partialLoop()
->setObjectKey('object')
->partialLoop('todos/_row.phtml', $childrenTodos);

Wordpress: Accessing A Plugin's Function From A Theme

I'm trying to add some functionality from a plugin I have made into a Wordpress theme but I am having little joy. The documentation doesn't really help me solve the problem so perhaps someone here can help.
I have a plugin in Wordpress that is activated and working fine. The class for this plugin has a function called generateHtml which I would like to access from a Wordpress Theme. But whatever I try, I cannot seem to access my plugin's code.
Can either give me a summary of what I need to do to get a theme accessing code from a plugin and/or point out there I am going wrong in my code:
Plugin:
<?php
/** Usual comments here **/
if (!class_exists("ImageRotator")) {
class ImageRotator {
private $uploadPath = '';
private $pluginPath = '';
private $options;
function __construct() {
$this->uploadPath = dirname(__file__).'\\uploads\\';
// add_shortcode('imagerotator', array(&$this, 'generateHtml'));
}
// Various functions for plugin
function generateHtml() {
echo '<p>Hello World</p>';
}
}
}
/**
* Create instance of image rotator
*/
$imageRotator = new ImageRotator();
/**
* Create actions & filters for Wordpress
*/
if (isset($imageRotator)) {
// Actions
add_action('admin_menu', array(&$imageRotator, 'createMenu'));
add_action('admin_init', array(&$imageRotator, 'registerSettings'));
add_action('imagerotator_show', array(&$imageRotator, 'generateHtml'));
}
Portion from theme header page:
<?php if (isset($imageRotator)) {
$imageRotator->generateHtml();
} else if (isset($ImageRotator)) {
print_r($ImageRotator);
} else {
echo '<p>Nope!</p>';
}
if (function_exists("imagerotator_show")) {
echo 'Function found';
} else {
echo 'Function NOT found';
}
?>
Currently all I ever see is "Nope" and "Function NOT found". Thanks for any input.
Lee,
For starters, "imagerotator_show" is not a function; it's the name of a type of action. When you use the add_action() function, Wordpress just adds your method to the list of functions/methods to call when a particular action is triggered. Thus your second test will always respond with 'Function NOT found'.
The most likely cause of the first problem is failing to declare the method you want to call as a public method. You're also making the code harder than it needs to be.
The best practice I've seen for declaring methods and registering hooks from a class looks something like this:
if ( ! class_exists( 'Foo' ) ):
class Foo {
function __construct() {
add_action( 'hook_name', array( &$this, 'my_hook_implementation' ) );
}
function my_hook_implementation() {
// does something
}
public function my_special_method() {
// does something else
}
}
if ( class_exists( 'Foo' ) ):
$MyFoo = new Foo();
This allows your class to keep all of its implementation details private. When you need to call my_special_method(), you do it as follows:
$MyFoo->my_special_method();
#andrew since I can't comment I thought I would answer your ancillary question. See:
http://net.tutsplus.com/tutorials/wordpress/create-wordpress-plugins-with-oop-techniques/
Where it is explained that when defining a callback function from an object you have to use the array function. It's basically saying get the function 'my_hook_implementation' from the object $this and use it as the callback parameter to the add action hook. It is because you defined the function within the scope of the object and you have to define the scope in order for PHP to know what function you are talking about. The scope being the object referred to by the variable $this.
You just need to use do_action() function, inside your theme.
If you want the function generateHtml to appears inside your header.php you just need to open the header.php file and paste <?php do_action('imagerotator_show'); ?> where you want and then your function will be called there.