0

I need to get the html code from the joomla internal link, and transfer the code to the module

My code default.php

<?php
defined( '_JEXEC' ) or die;
?>
    <div class="container" id="expandAjaxReq-root" class="<?php echo $moduleclass_sfx; ?>">
        <div class="row" id="expandAjaxReq-row-button">
            <div class="col-md-12"><button class="btn" id="btnExpaneAjaxReq" type="button" onclick="btnExpaneAjaxReqClick();"><strong><?php echo $params->get('btntext') ; ?></strong><i class="fas fa-chevron-down" style="margin-left: 10px;"></i><br></button></div>
        </div>
        <div class="row" id="expandAjaxReq-row-content">
            <div class="col-md-12" id="expandAjaxReq-col-content">
                <div class="d-flex overflow-auto" id="expandAjaxReq-content"></div>
            </div>
        </div>
    </div>

helper.php

<?php
defined( '_JEXEC' ) or die;
class modExpandAjaxReqHelper
{
    public  static function getRequestAjax() {
        jimport( 'joomla.application.module.helper' );
        $module = JModuleHelper::getModule('mod_expandajaxreq');
        $params = new JRegistry($module->params);
        $url     = $params->get('request_url');
        $html = file_get_contents('http://127.0.0.1/'. $url.'&tmpl=component'));
        return $html;
    }
}
?>

expandAjaxReq.js

function btnExpaneAjaxReqClick() {
    if (jQuery("#btnExpaneAjaxReq>i").hasClass( "fa-chevron-down" ))
    {
        jQuery("#btnExpaneAjaxReq>i").removeClass( "fa-chevron-down" );
        jQuery("#btnExpaneAjaxReq>i").addClass( "fa-chevron-up" )
        jQuery.ajax({
            url: 'index.php?option=com_ajax&module=expandajaxreq&method=getRequest&format=raw',
            type: 'POST',
            success:function(responce){
                jQuery("#expandAjaxReq-content").html(responce);                
            },
            error:function(responce){
                jQuery("#expandAjaxReq-content").html(responce.responseText);
            }
        });         
    }
    else {
        jQuery("#btnExpaneAjaxReq>i").removeClass( "fa-chevron-up" );
        jQuery("#btnExpaneAjaxReq>i").addClass( "fa-chevron-down" )
        jQuery("#expandAjaxReq-content").html("");
    }
}

How to read the hpml code correctly $html = file_get_contents('http://127.0.0.1/'. $url.'&tmpl=component')); ? How to get the root link of my site - http(s)://127.0.0.1/ ? Thank you so much

2

How to get the root link of my site

Joomla\CMS\Uri\Uri::base();

This prepend the scheme, host and port information


But your solution is not good!

You are making a http request to the server 2 times - this is not optimal!!! Making request from the php may work incorrectly if the link is closed from unauthorized users, or answer include information related by current user.

If you publish a module on the page more than once, then you need to separate requests based on the module ID

This code does not take into account the presence of a GET-parametrs and host in the $request_url

$html = file_get_contents('http://127.0.0.1/'. $url.'&tmpl=component'));

A request with the tmpl = component parameter will return the page with the html and head tags

A more correct way

  1. Build coorect request url in your module
// Parse url from module params
$uri = new \Joomla\CMS\Uri\Uri($url);

// Add var tmpl
$uri->setVar('tmpl', 'component');

// Build correct query
$request_url = $uri->toString();
  1. Set request url into joomla javascript options
// Include core.js
JHtmlBehavior::core();

// Put option with curent module id
(Joomla\CMS\Document\Document::getInstance())->addScriptOptions('mod_mymodulename.'.$module->id.'.request_url', $request_url);
  1. Pass a module id as a parameter in a button and add module id into id of button
<div class="col-md-12"><button class="btn" id="btnExpaneAjaxReq<?php echo $module->id; ?>" type="button" onclick="btnExpaneAjaxReqClick(<?php echo $module->id; ?>);"><strong><?php echo $params->get('btntext') ; ?></strong><i class="fas fa-chevron-down" style="margin-left: 10px;"></i><br></button></div>

and set id for target container

<div class="d-flex overflow-auto" id="expandAjaxReq<?php echo $module->id; ?>-content"></div>
  1. Write javascript for get request_url from Joomla.options and parse response
function btnExpaneAjaxReqClick(module_id) {
    if(!module_id) return;

    var module_btn = jQuery("#btnExpaneAjaxReq"+module_id+">i");

    if(!module_btn) return;
    
    var module_content = jQuery("#btnExpaneAjaxReq"+module_id+"-content");
    
    if(!module_content) return;

    var module_options = Joomla.getOptions('mod_mymodulename');

    if(!module_options[module_id]['request_url']) return;


    if (module_btn.hasClass( "fa-chevron-down" ))
    {
        module_btn.removeClass( "fa-chevron-down" );
        module_btn.addClass( "fa-chevron-up" )
        jQuery.ajax({
            url: module_options[module_id]['request_url'],
            type: 'GET',
            success:function(responce){
                module_content.html(responce.match(/<body.*?>(.|\s)+<\/body>/)[0]);
            },
            error:function(responce){
                module_content.html(responce.responseText);
            }
        });
    }
    else {
        module_btn.removeClass( "fa-chevron-up" );
        module_btn.addClass( "fa-chevron-down" )
        module_content.html("");
    }
}
2
  • Thanks a lot.I will be at work on Tuesday - I will try your code. Thank you again
    – antarey
    Jun 19 at 17:37
  • @ant did this answer resolve your question? If not, do you have feedback for this volunteer?
    – mickmackusa
    Jun 23 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.