Accessing custom attributes on a custom field in JFormField - php

I've created a custom field type in Joomla and need to pass parameters to it. For example, my JForm XML file looks like so:
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset addfieldpath="/administrator/components/com_gallery/models/fields">
<field name="images"
type="MultiImage"
label="Images"
description=""
imagetable="#__gallery_images"
imagedir="../images/gallery/originals/"
/>
</fieldset>
</form>
And I want to access the imagetable and imagedir attributes within my custom field:
<?php
// No direct access to this file
defined('_JEXEC') or die;
jimport('joomla.form.formfield');
class JFormFieldMultiImage extends JFormField
{
protected $type = 'MultiImage';
public function getInput() {
//this is where i want to access it
$input = $this->imagetable;
return $input;
}
}
I assumed you just used $this->attributename, and when I var_dump($this) I can see the attributes are defined but they are :protected.
I would appreciate some help on this :)
Thanks,
Tom

You are so close! Try this and let me know if it works for you, because it works for me. (Joomla 2.5.6)
echo $this->element['imagedir'];
echo $this->element['imagetable'];

Related

Joomla Component: View: Get values from field

I'm new to Joomla, especially to component development. Anyway, here's my question:
site\views\plaingallery\tmpl\default.xml
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="COM_PLAINGALLERY_PLAINGALLERY_VIEW_DEFAULT_TITLE">
<message>
<![CDATA[COM_PLAINGALLERY_PLAINGALLERY_VIEW_DEFAULT_DESC]]>
</message>
</layout>
<fields name="request"
addfieldpath="/administrator/components/com_plaingallery/models/fields">
<fieldset name="request">
<field name="galleryFolder" type="folderlist" default="" recursive="true"
label="Select a folder" directory="images" filter="" exclude="" width="300"
hide_none="true" hide_default="true" stripext="" />
</fieldset>
</fields>
</metadata>
site\views\plaingallery\view.html.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
/**
* HTML View class for the PlainGallery Component
*/
class PlainGalleryViewPlainGallery extends JViewLegacy
{
// Overwriting JView display method
function display($tpl = null)
{
// Assign data to the view
$this->msg = 'I am new to Joomla';
// Display the view
parent::display($tpl);
}
}
My question is: How do I access the value from the field[name="galleryFolder"] the user provided in the menu configuration?
Thanks for your help! I really do appreciate it.
This parameter is located in the query variable of the menu item.
You can try this for example:
$app = JFactory::getApplication();
/* Default Page fallback*/
$active = $app->getMenu()->getActive();
if (NULL == $active) {
$active = $app->getMenu()->getDefault();
}
if ( isset($active->query['galleryFolder']) ) {
$galleryFolder = $active->query['galleryFolder'];
}

Joomla throwing error for custom validation rule

I'm a beginner to the Joomla and trying to develop a component, but when I try to add rules to my backend panel, I keep getting this error. If anybody can tell me what I'm doing wrong, will be appreciated.
Here's the code for the rule which I've written.
com_mycomponent/models/rules/segment_name.php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* Form Rule class for the Joomla Framework.
*/
class JFormRuleSegmentName extends JFormRule
{
/**
* The regular expression.
*
* #access protected
* #var string
* #since 2.5
*/
protected $regex = '^[^0-9]+$';
}
And this is my form code:
com_mycomponent/models/forms/segment.xml
<?xml version="1.0" encoding="utf-8"?>
<form addrulepath="/administrator/components/com_mycomponent/models/rules">
<fieldset>
<field
name="id"
type="hidden"
/>
<field
name="segment_name"
type="text"
label="COM_MYCOMPONENT_SEGMENT_NAME_LABEL"
description="COM_MYCOMPONENT_SEGMENT_NAME_DESC"
size="40"
class="inputbox"
validate="segment_name"
required="true"
default=""
/>
</fieldset>
</form>
It is not working for me, this is what I get when I try to add or edit a new segment:
Error:
An error has occurred.
0 JForm::validateField() rule segment_name missing.
There are some rules that are not followed
You have used segment_name as file name but you have given SegmentName as class name. Both should match.
Not mandatory but you can use in the rules file
jimport('joomla.form.formrule');
defined('_JEXEC') or die('Restricted access');
// import Joomla formrule library
jimport('joomla.form.formrule');
/**
Form Rule class for the Joomla Framework.
*/
class JFormRuleSegment_rule extends JFormRule
{
In your form the addrule path should be given like this
<form>
<fieldset name="form_name" addrulepath="components/com_mycomponent/models/rules">
----------------fields
</fieldset>
</form>
form_name should be your form name
The suffix of the classname (JFormRule[SUFFIX]) must be the same as the rule file name. https://docs.joomla.org/Server-side_form_validation

Magento 2 - get scopeconfig values

I'm working on Magento 2.
But couldn't find the solutions for getting scopeconfig values in layout xml files.
In magento 1.x, using like below.
<block type="cms/block" ...>
<action method="..." ifconfig="config_path/config"></action>
</block>
In magento 2, how to use "ifconfig" in the layout xml?
It's same with magento 1.x.
You can use like below.
<block class="Magento\Framework\View\Element\Html\Link\Current" ifconfig="catalog/seo/search_terms" name="search-term-popular-link">
You can simply use like as
<block class="Ced\Abhinay\Block\Account\Active" ifconfig="ced/account/activation" name="ced_account_activation">
Where
Ced = Your Namespace
Abhinay = Your Module Name
Method1: Using Object Manager
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager
->get('Magento\Framework\App\Config\ScopeConfigInterface')
->getValue('section_id/group_id/field_id');
echo $conf;
?>
Method2: Using Helper
Create Data.php inside Helper folder of your module and write below code inside it.
<?php
namespace VendorName\ModuleName\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function getConfig($config_path)
{
return $this->scopeConfig->getValue(
$config_path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
?>
You can call this helper inside your phtml file by below code-
<?php
$value=$this->helper('Megha\Menu\Helper\Data')->getConfig('section_id/group_id/field_id');
echo $value;
?>
You can use like below.
<block class="Magento\Rss\Block\Feeds" ifconfig="rss/config/active" name="head_rss">
You can get directly scop config value into phtml file using below code.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager
->get('Magento\Framework\App\Config\ScopeConfigInterface')
->getValue('group/field/value');
Second Way to Created function for getting configuration values in your custom module's helper
<?php
namespace Vendor\Module\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function getConfig($config_path)
{
return $this->scopeConfig->getValue(
$config_path,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
Then you can get the configuration values to call this function in any phtml files.
$this->helper('Vendor\Module\Helper\Data')->getConfig('section/group/field');
Note: Please refer below links.
https://magento.stackexchange.com/questions/84481/magento-2-how-to-get-the-extensions-configuration-values-in-the-phtml-filesemphasized text

how to include public resources to typo3 extbase extension

I'm building an extension that creates a backend module that enables be_users to resize images.
I'm trying to add / include css and javascript files by using the pageRenderer but the files are never included I can only apply css if add it directly in the fluid Template using a style tag and include the javascript file with a script tag.
I tried something like this in the controller
protected $pageRenderer;
....
$this->pageRenderer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Page\\PageRenderer');
$this->pageRenderer->addCssFile('/typo3conf/ext/extKey/Resources/Public/css/styles.css');
$this->pageRenderer->loadJquery();
also tried with a viewHelper
namespace Vendor\ExtKey\ViewHelpers;
class AddJsFileViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Be\AbstractBackendViewHelper {
public function render() {
$doc = $this->getDocInstance();
$pageRenderer = $doc->getPageRenderer();
$pageRenderer->loadJquery();
}
}
and in my tempate
{namespace pager=Vendor\ExtKey\ViewHelpers}
<f:layout name="Default" />
<f:section name="main">
<pager:addJsFile />
...
still nothing
I'm not sure how you define the template for your backend, but it seems this usually happens using the backend container view helper which already has functions for that:
<f:be.container
addCssFile="{f:uri.resource(path:'css/style.css')}"
addJsFile="{f:uri.resource(path:'js/scripts.js')}">
[your templates content]
</f:be.container>
In TYPO3 7.6.X, It has to be like following
<f:be.container
includeCssFiles="{style:'{f:uri.resource(path:\'css/style.css\')}'}"
includeJsFiles="{script:'{f:uri.resource(path:\'js/script.js\')}'}"
>
<!-- Template Code -->
</f:be.container>
As includeCssFiles and includeJsFiles requires array to be passed, we
can include any number of js and css.
i think the problem was my ViewHelper need to renderChilden and start/end page
current implementation is like this
the ViewHelper
namespace Vendor\ExtKey\ViewHelpers;
class AddPublicResourcesViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Be\AbstractBackendViewHelper {
public function render() {
$doc = $this->getDocInstance();
$pageRenderer = $doc->getPageRenderer();
$extRelPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extRelPath("ext_key");
$pageRenderer->addCssFile($extRelPath . "Resources/Public/css/styles.css");
$pageRenderer->loadJquery();
$pageRenderer->addJsFile($extRelPath . "Resources/Public/js/app.js");
$output = $this->renderChildren();
$output = $doc->startPage("title") . $output;
$output .= $doc->endPage();
return $output;
}
}
the template
{namespace pager=Vendor\ExtKey\ViewHelpers}
<f:layout name="Default" />
<f:section name="main">
<pager:addPublicResources />
Pagerender::loadJjquery is working and accessible like this
TYPO3.jQuery(function($) {
});

Different form formatter for embeded form?

I'm trying to change the form formatter of the embeded form. Is it possible to approach something like this?
class sfOuterForm extends sfForm {
public function configure()
{
$innerForm = new sfForm();
$this->embedForm('inner', $innerForm);
$this->getWidgetSchema()->setFormFormatter('list');
$this->getEmbeddedForm('inner')->getWidgetSchema()->setFormFormatterName('table');
}
}
i'm expecting the following:
echo (new sfOuterForm())
outputs:
<li><label>Outer Label</label><input type="text" /></li>
<li>
<table>
<tr><td><label>Inner Label</label></td><td><input type="text" /></td></tr>
</table>
</li>
Once a form is embedded, it's original widget schema and validator schema do nothing - they've been merged into the top level schemas. Thus, you need to set the form formatter before embedding:
$this->getWidgetSchema()->setFormFormatter('list');
$innerForm = new sfForm();
$innerForm->getWidgetSchema()->setFormFormatterName('table');
$this->embedForm('inner', $innerForm);
It's worth a look into sfForm::embedForm to see what's going on internally.
I'll answer my question by myself :)
The problem arised when i tried to change formatter for relation's embedded forms. I solved this as follows:
class sfOuterForm extends sfForm {
public function configure()
{
$innerForm = new sfForm();
$this->embedRelation('relationName');
$this->getWidgetSchema()->setFormFormatter('list');
$this->getEmbeddedForm('relationName')->getWidgetSchema()->setDefaultFormFormatterName('table');
}
}
Hope this will help someone :)

Categories