PDA

Просмотр полной версии : Использование функций класса class.tx_ttnews.php в своем расширении


shuman
16.05.2011, 13:31
При создании своего расширения нужно составить список категорий новостей.

Это простая задача, если не брать во внимание подкатегории.

Для учета подкатегорий решил подключить класс class.tx_ttnews.php и заюзать полезную функцию initCategoryVars:

<?php
/************************************************** *************
* Copyright notice
*
* (c) 2011
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
************************************************** *************/
/**
* [CLASS/FUNCTION INDEX of SCRIPT]
*
* Hint: use extdeveval to insert/update function index above.
*/

require_once(PATH_tslib.'class.tslib_pibase.php');
require_once(t3lib_extMgm::extPath('tt_news') . 'pi/class.tx_ttnews.php');


/**
* Plugin 'tt_news_change_picture' for the 'tt_news_change_image' extension.
*
* @author
* @package TYPO3
* @subpackage tx_ttnewschangeimage
*/
class tx_ttnewschangeimage_pi1 extends tslib_pibase {
var $prefixId = 'tx_ttnewschangeimage_pi1'; // Same as class name
var $scriptRelPath = 'pi1/class.tx_ttnewschangeimage_pi1.php'; // Path to this script relative to the extension dir.
var $extKey = 'tt_news_change_image'; // The extension key.
var $pi_checkCHash = true;
var $cObj; // The backReference to the mother cObj object set at call time

/**
* The main method of the PlugIn
*
* @param string $content: The PlugIn content
* @param array $conf: The PlugIn configuration
* @return The content that is displayed on the website
*/
function main($content, $conf) {
$this->cObj=t3lib_div::makeInstance('tslib_cObj');
$this->conf = $conf;
$this->pi_loadLL(); // Loading language-labels
$this->pi_setPiVarDefaults(); // Set default piVars from TS
$this->pi_initPIflexForm(); // Init FlexForm configuration for plugin

$tt_news = new tx_ttnews;
$tx_news->cObj = t3lib_div::makeInstance('tslib_cObj');
$tt_news->enableFields = $tx_news->cObj->enableFields('tt_news');
// initialize category vars
$tt_news->initCategoryVars(); <-эта строка вызывает ошибку

$content='';

return $this->pi_wrapInBaseClass($content);
}

}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/tt_news_change_image/pi1/class.tx_ttnewschangeimage_pi1.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/tt_news_change_image/pi1/class.tx_ttnewschangeimage_pi1.php']);
}

?>

это вызывает ошибку:

Fatal error: Call to a member function enableFields() on a non-object in /usr/local/apache2/htdocs/..мой сайт../typo3conf/ext/tt_news/pi/class.tx_ttnews.php on line 2730

Вызвано это тем что в class.tx_ttnews.php данная функция enableFields вызывается таким образом:

код расширения tt_news
$this->enableCatFields = $this->cObj->enableFields('tt_news_cat');Получается что родительский объект cObj не инициализируется, хотя я явно задал его?

Я слабо разбираюсь в особенностях объектного программирования в php.

reva2
17.05.2011, 02:02
Первое что бросилось в глаза:
$tt_news = new tx_ttnews;
а должно быть
$tt_news = new tx_ttnews();

shuman
17.05.2011, 06:52
Сделал. Ошибка та же.

Пример взят из файла fe_index.php который был в версии tt_news 3.0

http://typo3.org/extensions/repository/new/tt_news//info/pi%252Ffe_index.php/?tx_terfe_pi1%5BdownloadFile%5D=ext_icon__x.gif&cHash=e539286dac

Придется без подкатегорий писать расширение. Неохота кучу функций копировать из class.tx_tt_news.php

shuman
17.05.2011, 07:33
нашел решение в news_calendar http://typo3.org/extensions/repository/view/newscalendar/current/info/pi1%252Fclass.tx_newscalendar_pi1.php/

там это сделано так:
if ( $this->categorySelection && $this->useSubCategories ) {
$this->categorySelection .= ',' . tx_ttnews_div::getSubCategories( $this->categorySelection );
}

видимо считается что объект tx_ttnews_div уже существует

shuman
17.05.2011, 09:38
Виноват - версия tt_news 3.0.1 и есть последняя версия несмотря на 2009 год.

А у меня была старая - 2.5

В ней не было этой функции

dmartynenko
22.05.2011, 17:03
На всякий случай - проблема с cObj решается просто, у вас в коде опечатка - вместо $tt_news указано $tx_news.

Также можно использовать $tt_news->cObj = $this->CObj;

shuman
23.05.2011, 04:52
На всякий случай - проблема с cObj решается просто, у вас в коде опечатка - вместо $tt_news указано $tx_news.

Также можно использовать $tt_news->cObj = $this->CObj;

Спасибо!