{"id":450,"date":"2014-06-27T11:58:22","date_gmt":"2014-06-27T09:58:22","guid":{"rendered":"http:\/\/self.mestrona.net\/blog\/?p=450"},"modified":"2014-08-03T18:38:26","modified_gmt":"2014-08-03T16:38:26","slug":"rewrite-classes-in-contao","status":"publish","type":"post","link":"https:\/\/s3lf.de\/blog\/2014\/rewrite-classes-in-contao\/","title":{"rendered":"Rewrite Classes in Contao"},"content":{"rendered":"<p>The <a href=\"http:\/\/www.contao.org\/\">Contao Web CMS System<\/a>\u00a0provides the possibility to rewrite classes of the core or of 3rd party modules.<\/p>\n<p>In case you want to publish your extension, this is not a suggested way because this can create conflicts if two installed modules try to rewrite the same class. But for local modules this is a valid method.<\/p>\n<p>First of all you must ensure, that your module is loaded after the module you want to rewrite. You can assure this by giving your module a name which comes alphabetically after the name of the module to be rewritten. Or you can add a line<\/p>\n<pre style=\"color: #000000;\"><span style=\"font-weight: bold; color: #008000;\">requires<\/span>[] = <span style=\"font-weight: bold; color: #008000;\">\"dlh_googlemaps\"\r\n<\/span><\/pre>\n<p>to your autoload.ini. Digging into the code you find out that this require statement is\u00a0evaluated in\u00a0<code>\\Contao\\ModuleLoader::scanAndResolve<\/code>.<\/p>\n<p>Now you simply have to add your class to the <code>autoload.php<\/code>\u00a0with the same class name (without namespace prefix) as the class to be rewritten:<\/p>\n<pre style=\"color: #000000;\">ClassLoader::<span style=\"font-style: italic;\">addClasses<\/span>(<span style=\"font-weight: bold; color: #000080;\">array\r\n<\/span>(\r\n\t<span style=\"font-weight: bold; color: #008000;\">'Mymodule\\Googlemap' <\/span>=&gt; <span style=\"font-weight: bold; color: #008000;\">'system\/modules\/mymodule\/classes\/Googlemap.php'<\/span>,\r\n));<\/pre>\n<p style=\"color: #000000;\">The class loader uses the <em>class_alias()\u00a0<\/em>PHP function to define an alias for\u00a0the class name without the namespace. So now the class can be instantiated using <code>\\Googlemap<\/code>.<\/p>\n<p style=\"color: #000000;\">Your <code>Googlemap.php<\/code> would look like this<\/p>\n<pre><span style=\"font-weight: bold; color: #000080;\">namespace <\/span>Mymodule;\r\n\r\n<span style=\"font-weight: bold; color: #000080;\">class <\/span>Googlemap <span style=\"font-weight: bold; color: #000080;\">extends <\/span>\\delahaye\\googlemaps\\Googlemap {\r\n    <span style=\"font-weight: bold; color: #000080;\">public static function <\/span>getMapData(<span style=\"color: #660000;\">$intMap<\/span>, <span style=\"color: #660000;\">$strFormat<\/span>= <span style=\"font-weight: bold; color: #008000;\">''<\/span>, <span style=\"color: #660000;\">$arrParams<\/span>=<span style=\"font-weight: bold; color: #000080;\">false<\/span>) {\r\n        <span style=\"font-weight: bold; color: #000080;\">parent<\/span>::<span style=\"font-style: italic;\">getMapData<\/span>(<span style=\"color: #660000;\">$intMap<\/span>, <span style=\"color: #660000;\">$strFormat<\/span>, <span style=\"color: #660000;\">$arrParams<\/span>);\r\n        \/\/ your code\r\n    }\r\n}<\/pre>\n<p style=\"color: #000000;\">If you set<\/p>\n<pre><span style=\"color: #660000;\">$GLOBALS<\/span>[<span style=\"font-weight: bold; color: #008000;\">'TL_CONFIG'<\/span>][<span style=\"font-weight: bold; color: #008000;\">'debugMode'<\/span>] = <span style=\"font-weight: bold; color: #000080;\">true<\/span>;\r\n<\/pre>\n<p style=\"color: #000000;\">in your <code>localconfig.php<\/code>, a debug area is displayed on the page&#8217;s bottom indicating\u00a0which classes are aliased:<\/p>\n<p style=\"color: #000000;\">\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-457\" src=\"http:\/\/s3lf.de\/blog\/wp-content\/uploads\/2014\/06\/Screenshot_Selection_0061-300x57.png\" alt=\"Screenshot_Selection_006\" width=\"811\" height=\"154\" srcset=\"https:\/\/s3lf.de\/blog\/wp-content\/uploads\/2014\/06\/Screenshot_Selection_0061-300x57.png 300w, https:\/\/s3lf.de\/blog\/wp-content\/uploads\/2014\/06\/Screenshot_Selection_0061-624x119.png 624w, https:\/\/s3lf.de\/blog\/wp-content\/uploads\/2014\/06\/Screenshot_Selection_0061.png 929w\" sizes=\"auto, (max-width: 811px) 100vw, 811px\" \/><\/p>\n<p style=\"color: #000000;\">The method\u00a0<code>\\Contao\\ClassLoader::findClass()<\/code>\u00a0is responsible for searching the registered namespaces for the class. The static field <code>ClassLoader::$namespaces<\/code>\u00a0contains the registered namespaces in reverse order of the loading order of the modules so the first match is picked.<\/p>\n<p style=\"color: #000000;\">There is one catch: if two or more of your modules use the same namespace, all of them have to be loaded before the module you want to overwrite (using the required-configuration mentioned above) . Otherwise the namespace will be listed in the wrong order as each namespace can be only registered once and the overwrite will not have an effect.<\/p>\n<p style=\"color: #000000;\">References:<\/p>\n<ul>\n<li><a href=\"https:\/\/contao.org\/files\/conference\/2012\/papers\/Entwickler-Workshop.pdf%20\">https:\/\/contao.org\/files\/conference\/2012\/papers\/Entwickler-Workshop.pdf<\/a>\u00a0(German)<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The Contao Web CMS System\u00a0provides the possibility to rewrite classes of the core or of 3rd party modules. In case you want to publish your extension, this is not a suggested way because this can create conflicts if two installed modules try to rewrite the same class. But for local modules this is a valid [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,53,54],"tags":[],"class_list":["post-450","post","type-post","status-publish","format-standard","hentry","category-contao-2","category-english-2","category-oss-2"],"_links":{"self":[{"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":9,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"predecessor-version":[{"id":465,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/posts\/450\/revisions\/465"}],"wp:attachment":[{"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}