How handle required JS file

Dear group,

I've regularly registered a amd plugin, say in myplugin/rewrite/myamd.js .

Now I need improve this with some php, so I've tried the follow:

  1. renamed myamd.js in myamd.php and then add some php code to echo my needs
  2. added in /rewrite/ a .htaccess that contains RewriteRule ^(.*)\.js$ $1.php

That's work good for regular php function, but if I try to use some elgg built in as 

elgg_get_plugin_from_id('mypluginId')->getAllSettings();

In error log appears 

PHP Fatal error:  Call to undefined function elgg_get_plugin_from_id()

So I've attempted to handle this page as normal page to require this php via start.php, but neither

elgg_register_page_handler('myplugin', 'myplugin_page_handler');

nor

elgg_register_plugin_hook_handler('route', 'myplugin', 'myplugin_utility_routes_wrap');

intercepts myplugin/rewrite/myamd.js  . 

I think that's due to the elgg_define_js() routine. 

 

Someone can help me to achieve this goal?

 

 

 

  • Not exactly... my goal is use hybrid php-javascript amd module. 

    I know what you suggest but according to documentations,  using elgg_register_simplecache_view:

     If the view is called by the engine/handlers/cache_handler.php file, the Elgg engine will not be loaded and the contents of the view will returned from file.

     I would generate dinamically this file, so that I could, for example, check if user is admin, else return a simple json:

    {"msg": "error, you're not admin!"};

    That's only one example about what my question covers.

  • Can you describe your needs in more detail? What are the pieces on information that may be different for each user?

    Could you simply do something like:

    <?php
    
    function muplugin_init() {
        if (elgg_is_admin_logged_in()) {
            elgg_require_js('myplugin/admin_user.js');
        } else {
            elgg_require_js('myplugin/regular_user.js');
        }
    }
    
  • My explanations wasn't clear, sorry...

    For Example in myplugin/rewrite/myamd.php something like:

    <?php
    
    if (elgg_is_admin_logged_in()) {
       $write = '{"msg":"admins", "data":"data that only admin can receive as required plugin", "time":"' . time() . '"}';
    } else {
       $write = '{"msg":"admins", "data":false, "time": "' . time() . '"';
    }echo "define($write);"

    as you can see I need elgg usage to check if is admin an generate content on the fly since the time must be updated every call (that' a simple example, but substantially I need this skill to improve my complex goal), therefore content can't be stored in chache.

    Since requirejs can load only ".js" extension file, I use the .htaccess in my first post to rewrite the ".php" in ".js";

     

  • Is there a reason why the logic needs to be in the javascript file?

    Why not e.g. fetch the needed data via AJAX, checking the user's role on server side, and providing the response based on it?

    Or include the data to the page (html) already when it is being rendered on server side?

  • Because I need that this will be an amd module.

    As you suggest I can implement this creating a module that make an ajax call to an action page, that can generate the right content, and store the ajax response in module variable... my problem isn't achieve my purpose in some exotic manner, but if I could reach exactly my goal, with the method I've explained.

    Anyway, thanks for all reply... I hope to reach my goal!