Observer function is not redirecting to my custom page in Moodle - plugins

I am fairly new to Moodle. I am working on a custom plugin in which I wanted to make a pop up that shows after the user has logged in for the first time. Exploring the forum, I came up with the idea of catching event and triggering something. Basically I tried to catch the user_loggedin event and in the observer function I tried to trigger a redirection which has my pop up but it didn't seems to work properly. I am not getting any kind of error (debugging is on) so it is hard for me to troubleshoot the actual problem.
My db/events.php file:
<?php
/*
* #package local_message
* #author Kristian
* #license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$observer = array(
array(
'eventname' => '\core\event\user_loggedin',
'callback' => '\local_custom_signup\local_first_signup_observer::first_signup',
),
);
//var_dump($observer);
My classes/observer.php file:
<?php
/*
* #package local_message
* #author Kristian
* #license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_custom_signup;
defined('MOODLE_INTERNAL') || die();
class local_first_signup_observer{
public static function first_signup(\core\event\user_loggedin $event){
global $CFG;
redirect($CFG->wwwroot . '/local/custom_signup/signupform.php', get_string('cancelled_form', 'local_message'));
}
}

You cannot do that - event handlers cannot redirect to a new page, because Moodle is usually in the middle of making other changes (and there may well be other event handlers that are still wanting to have a go at handling that event).
That said, the most likely reason why you're not seeing this do anything, is that you've called your file "classes/observers.php", but called the class "local_first_signup_observer". Moodle has no way of guessing that it needs to look in a "observers.php" to find this class.
If you rename "observers.php" to "local_first_signup_observer.php" (and purge all caches), then it should find it (but it still won't do anything useful, as redirects aren't allowed here).

Related

How to change the position of a column in the TYPO3 TCA?

I want to move a column from one tab to another. Is there a better way than to rewrite the types definition manually which is not very reliant in regard to extension updates changing the TCA?
It's not 100% fail safe, since there still might be extensions messing things up, but the closest you can get will be to make use of the ExtTablesInclusionPostProcessing event and put addToAllTCAtypes into the invoke method, since this will not just add new fields but even put existing fields to another tab:
<?php
declare(strict_types=1);
namespace Vendor\Extension\EventListener;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
/**
* Class ExtTablesInclusionPostProcessing
*/
class ExtTablesInclusionPostProcessing
{
/**
* Function which may process data created / registered by extTables
* scripts (f.e. modifying TCA data of all extensions)
*
* #param AfterTcaCompilationEvent $event
*/
public function __invoke(AfterTcaCompilationEvent $event): void
{
ExtensionManagementUtility::addToAllTCAtypes('tt_content', 'fieldname', '', 'after:fieldname_in_the_other_tab');
}
}
To remove the existing field before, you could make use of this trick:
https://stackoverflow.com/a/59179916/2333754
And finally to be on the safer side you could make sure that other extensions are loaded before your own extension by putting them into the suggest section of your composer.json.
With installation of extension tcabuilder it is possible to do simple changes in the TCA.

TYPO3 CMS EXT:webkitpdf error CacheDatabaseBackend TYPO3 9.5

Because the famous extension webkitpdf isn't maintained anymore, I try my very best to get this working under TYPO3 9.5.
My patched version https://github.com/EnzephaloN/typo3-extension-webkitpdf worked fine til 8.7, but now I get an error in Classes/Utility/CacheDatabaseBackend.php::set
/**
* Saves data in a cache file.
*
* #param string $entryIdentifier An identifier for this specific cache entry
* #param string $data The data to be stored
* #param array $tags Tags to associate with this cache entry
* #param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
* #return void
*/
public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
if ($this->maximumNumberOfEntries > 0 && !$this->has($entryIdentifier)) {
$this->removeOldEntriesIfRequired();
}
parent::set($entryIdentifier, $data, $tags, $lifetime);
}
The parent::set($entryIdentifier, $data, $tags, $lifetime); results an Cannot call abstract method TYPO3\CMS\Core\Cache\Backend\BackendInterface::set() error.
Anyone an idea how to adjust this method to TYPO3 9.5??
Tried to extend from \TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend which caused a lot of trouble with different errors in backend.
For now I disabled calling parent::set(... - so it works without cache, but without errors.
In the BackendInterface the function set is defined like this:
public function set($entryIdentifier, $data, array $tags = [], $lifetime = null);
Nevertheless it never has a concrete function in class AbstractBackend in TYPO3 version 9. Probably in Version 8 that's different and the reason that it still worked.
So you've to define the function in your own class and it has to satisfy the requirements predefined in the BackendInterface like shown above.
To get an idea what the function is doing look in TYPO3 7 or 8. Even if you've to adjust it perhaps that might be useful.
EDIT:
In general to keep the call with parent::set(...); in your own function you still had to create another class. This is probably not required if you don't need the functionality additionally in another place.
In general you can just search in older versions what the function did there and integrate that functionality in your existing function set(...).

How to register modules for advanced functions

When I select Web/Functions in the menu of typo3 cms 9.5.4 backend, I get this error:
Advanced functions
No modules have been registered. Please contact your system administrator.
I am the system administrator. I can't find anywhere how to register modules. How do I register any modules?
Like Peter wrote, the Extension func was removed from core, is actually not marked as compatible with version 9.5. and should avoided to use further more.
But follow two files will help you to register your own module:
ext/extension/ext_tables.php
// Module wizard
if (TYPO3_MODE === 'BE') {
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::insertModuleFunction(
'web_func',
\Vendor\Extension\MyModuleFunction::class,
null,
'LLL:EXT:extension/Resources/Private/Language/locallang_module.xlf:mymodulefunction'
);
}
ext/extension/Classes/MyModuleFunction.php
<?php
namespace Vendor\Extension;
class MyModuleFunction
{
/**
* Initialize the object
*
* #param \object $pObj A reference to the parent (calling) object
* #throws \RuntimeException
*/
public function init($pObj)
{
// Required method
}
/**
* Checking for first level external objects
*/
public function checkExtObj()
{
// Required method
}
/**
* Main function creating the content for the module.
*
* #return string HTML content for the module, actually a "section" made through the parent object in $this->pObj
*/
public function main()
{
return '<h1>My module function</h1>';
}
}
As far as I know, EXT:wizard_crpages and EXT:wizard_sortpages are not maintained anymore in TYPO3 9.x.
EXT:func has been moved to the TYPO3 Extension Repository to preserve the possibility to register your own wizards.
UPDATE:
The possibility to create multiple pages and to sort pages is now available via the context menu in the page tree. Just do a left or right click on the icon in front of any page and choose More options ... from the context menu.

TYPO3 extbase - get "uid" of non persisted object

Just started out with the frst extbase extension. In localconf I added these actions: 'list, new, show, create, edit'.
Default was that "create" redirected to "list" with no arguments, and that worked fine right after creation of the extension.
$this->redirect('list'); // <- this works if used
But instead of redirecting to "list" I would like to redirect to "show" to display the newly added priceFormCalc. A collegue helped me to acomplish this using persist.
Below is the code and it works. But reading on the net it seem that it should not be best practice to run persist. It should be doable to call the action show without manually persisting first.
So question is: Is this the correct way to do it, or is there a more "ext base" way show a newly created record?
public function createAction(\TYPO3\OgNovomatrixpricecalc\Domain\Model\PriceCalcForm $newPriceCalcForm) {
$this->priceCalcFormRepository->add($newPriceCalcForm);
$this->flashMessageContainer->add('Your new PriceCalcForm was created.');
// workaround - or the right way?
$persistenceManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Tx_Extbase_Persistence_Manager');
$persistenceManager->persistAll(); // <- save i database
$uid = $newPriceCalcForm->getUid(); // <- get UID of newly saved record
// do redirect using uid of newly created priceCalcForm
$this->redirect('show',Null,Null, array('priceCalcForm' => $uid));
}
To
You can persist the current state and then get the uid. Inject the configuration manager (TYPO3 6.x way):
/**
* persistence manager
*
* #var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
* #inject
*/
protected $persistenceManager;
Then use
$this->persistenceManager->persistAll();
to apply all changes. Then you can pass your object (or only the uid) to the action.

Zend Framework 1.11.12, Doctrine 2.2.2 integration: Error 500

I'm trying to integrate Doctrine 2 into Zend Framework (I'm new to ZF). I've look everywhere on the net but couldn't find my answer...
I've followed this recent tutorial: http://hectorpinol.com/zend-framework-1-11-and-doctrine-2-2-x-integration/ and I've managed to generate a table using the CLI.
The last step of the tuto is to add a new line in this table, simply using the Index controller. But my website doesn't work anymore (a brutal error 500, no message) because I changed the bootstrap.
if I remove the last lines I added to the _initDoctrine() method, it works again (but without Doctrine of course). Here they are:
// set the proxy dir and set some options
$config->setProxyDir(APPLICATION_PATH . '/models/Proxies');
$config->setAutoGenerateProxyClasses(true);
$config->setProxyNamespace('App\Proxies');
// now create the entity manager and use the connection
// settings we defined in our application.ini
$connectionSettings = $this->getOption('doctrine');
$conn = array(
'driver' => $connectionSettings['conn']['driv'],
'user' => $connectionSettings['conn']['user'],
'password' => $connectionSettings['conn']['pass'],
'dbname' => $connectionSettings['conn']['dbname'],
'host' => $connectionSettings['conn']['host']
);
$entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
// push the entity manager into our registry for later use
$registry = Zend_Registry::getInstance();
$registry->entitymanager = $entityManager;
return $entityManager;
Do you have any idea to unlock the situation? It's frustrating because I know I'm so close to make it work...
UPDATE1: I forgot to mention, in case it helps: I'm using WAMP on Windows. Thanks
UPDATE2: Added the parameters of the create() function.
UPDATE3: Actually it might not be an error 500. Chrome says this but Firefox just displays nothing. No answer from the server.
1) Change the environment to development to see the error message.
2) Use Bisna library instead, will save a lot of time (I don't see a point in integrating Doctrine manually well only educational purposes but you might want to save it for later).