Add CSS attributes to Symfony form labels? - css

I try to add some css attributes to labels in my custom sfForm but I can't achieve it.
In my custom class myForm extends sfForm, I create all textfields dynamically:
public function configure()
{
$widgetFixtures = array();
foreach ($fixtures as $fixture) {
$widgetFixtures[$fixture->getId()] = new sfWidgetFormInputText(
array('label' => $fixture->getTeamNameDom()),
// I would like to add something like: array('class' => $fixture->getCSS()),
array('value' => $fixture->getScore1(), 'readonly' => 'readonly')
);
}
$this->setWidgets($widgetFixtures);
}
I tried to format the rendering with setFormFormatterName but without success.
Note: I can't use renderLabel($value, $attributes = array()) in the template because I get the CSS class from the DB (as you may have seen, I have to use: $fixture->getCSS()).
Could someone shed my light?
Many thanks.

Here is how I solved it.
I took both suggestions from johnwards and richsage and put them together :
"This sort of stuff should be handled in the view/action."
"Access to the options/attributes passed to the Widget itself."
First, I add the CSS class to the input itself (even if I will not use it).
In my custom class myForm extends sfForm,
foreach ($fixtures as $fixture) {
$widgetFixtures[$fixture->getId()] = new sfWidgetFormInputText(
array('label' => $fixture->getTeamNameDom()),
array('value' => $fixture->getScore1(),
'readonly' => 'readonly',
'class' => $fixture->getCSS())
);
}
Then, in the template, instead of using echo $form;, I add the CSS class to the label as shown below:
foreach ($form as $widgetId => $widget) {
...
$labelClass = $widget->getWidget()->getAttribute('class');
echo '<td>'.$widget->renderLabel(null, array('class' => $labelClass)).'</td>';
...
}
Maybe it is not the best way to solve this issue but it works.
Thanks all for your feedback!

What you're trying to do seems possible but your greyed out syntax seems a little off. Try this:
$widgetFixtures[$fixture->getId()] = new sfWidgetFormInputText(
array('label' => $fixture->getTeamNameDom()),
array('class' => $fixture->getCSS()),
array('value' => $fixture->getScore1(), 'readonly' => 'readonly')
);
... or check "The sfWidgetForm Base Class" section here: http://www.symfony-project.org/forms/1_4/en/A-Widgets

The class attribute needs to be passed in as an array in the second parameter for all form widgets.
$this->setWidget('foo', new sfWidgetFormInputText(array(),array('class','fooclass'));

You could try overriding the widget class if you want to add CSS to labels - specifically the methods that render the label. You could then do something like
$foo = new myWidgetFormInputText(array("myCSSClass" => $fixture->getCSS()));
and then override your renderLabel() method or similar in your widget. Your widget will have access to the options you pass in - in the above example myCSSClass is the option key. You can then apply this class value to your widget's label.

It's much easier than that. Just apply CSS to the label for tag...
label[for=payment_cardNumber]
{
margin-top: 20px;
color: red;
}

Related

Drupal 8 translating paragraphs programmatically

Paragraphs are supposed to be translated on on the level of their component fields, not on the paragraph_field level. So how do you programmatically translate paragraphs?
To be more explicit, my paragraph field is not translatable, but the component fields are. So how can I load a node, loop through the paragraph items, and add translations to the fields?
Does anyone have an example?
Thanks.
following https://www.flocondetoile.fr/blog/translate-programmatically-drupal-8 node translation:
This is an abstraction of my actual code, and I haven't actually tested it:
$node = node_load(12);
if ($node->hasTranslation('de')) {
$transl_node = $node->getTranslation('de');
foreach ($transl_node->field_paragraph => $paragraph) {
$entity_array = $paragraph->toArray();
$translated_fields = [];
$translated_fields['field_body'] = array(
'value' => 'translated value',
'format' => 'full'
);
$translated_fields['field_section_title'] = 'translated section title';
$translated_entity_array = array_merge($entity_array, $translated_fields);
if (!$paragraph->hasTranslation('de')) {
$paragraph->addTranslation('de', $translated_entity_array);
$paragraph->save();
}
}
$transl_node->save();
}

Set the class of all Input Boxes in Yii2

I have a web application designed in Yii2. The client wants all the text boxes to be shorter/smaller. In bootstrap, in addition to "form-control", I'd need to add the "input-sm" class as well.
However, as far as Yii2 is concerned, the form-control class is set in the model. Example: ActiveField model's has
'inputOptions' => [
'class' => 'form-control',
],
However, I am using various widgets and extensions and need a supported solution to set the class such that with a few edits, I can make all text boxes and other controls look smaller. I don't want to mention a custom inputOptions in every form.
One solution could be to extend all the models I use and setting the class (input-sm) there, but would prefer a CSS or simpler solution.
Extend Your Active Field Class as:-
<?php
namespace auction\widgets;
use yii\helpers\ArrayHelper;
class ActiveField extends \yii\widgets\ActiveField{
//Error Options For Active Field Error Tag
public $errorOptions= ['class' => 'error', 'tag' => 'span'];
public function init(){
//Changing Input Options Merge with form-class
$this->inputOptions = ['placeHolder' => $this->model->getAttributeLabel($this->attribute), 'class' => 'input-sm'];
parent::init();
}
/**
* #param null $label Setting Label Value to false
* #param array $options
*/
public function label($label = null, $options = [])
{
$this->parts['{label}'] = '';
}
}
now use ActiveForm As:-
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'fieldClass' => 'auction\widgets\ActiveField',
'successCssClass' => false,
'options'=> ['role' => 'form']]); ?>

Adding style to CakePHP automatically created input div

CakePHP creates a div that automatically wraps any of its input tags that are built with the formhelper as followed:
$this->formhelper->input('something');
Such that the output looks about as followed:
<div class='input'>
<input />
</div>
I know that there is a way to add classes to the input tag i.e.
$this->formhelper->input('text', array('class' => 'some_css'));
But how would you add a style to the div that is automatically created by CakePHP. This might be something where the core needs to be hacked, but I want to know if there is a better way to do this, so that I get something as followed:
<div class='input other_class_I_want_here'>
<input />
</div>
Thank you to anyone who can help.
Simply add a new class to the div.
$this->formhelper->input('text', array('div'=>array('class'=>'divClass'),'class' => 'some_css'));
should actually output
<div class='input divClass'>
<input class='other_class_I_want_here' />
</div>
The above answer is certainly correct. And works beautifully if you only require adding a class in a one (or a few) specific locations.
However, anyone arriving here looking for a way to add a class to input div wrappers application-wide (ex: if you're using a front-end framework which often requires a specific class name be added to input wrappers to enable auto-styles) there is a MUCH better solution. That being, a custom FormHelper.
In the App/View/Helper directory create and save a file "MySuperCoolFormHelper.php"
Place the following code in the file:
App::uses('FormHelper', 'View/Helper');
class MySuperCoolFormHelper extends FormHelper {
protected function _divOptions($options) {
if(isset($options['div'])
$options['div'] .= ' class1 class2 class3'; //note the prefixing space
else
$options['div'] = 'class1 class2 class3';
return parent::_divOptions($options);
}
}
To use this new form helper globally, add the following code to your AppController:
public $helpers = array(
'Form' => array(
'className' => 'MySuperCoolFormHelper'
)
//The rest of your helper inits
);
... and BLAMMO you're done!
CakePHP 3:
For applying 'form-group' to DIV and 'form-control' to the input field
<?=
$this->Form->control('year', [
'type' => 'select',
'value' => $year,
'options' => $years,
'label' => false,
'class' => 'form-control',
'templates' => ['inputContainer' => '<div class="form-group">{{content}}</div>']
]);
?>

How can I add css classes to specific symfony2 form choices?

I could do this with Javascript, but I was wondering if I could add a css class to specific symfony2 form choices (not the choice field itself, but the individual choices).
For example I want to apply different css styles to individual 'option' tags inside a 'select'. I could only find a way to add a class to the tag.
Thanks in advance.
I think you can simply do:
{{ form_widget(form.name, { 'attr' : { 'class' : 'myClass' } }) }}
... as explained here, without creating your own form style.
You can override the layout of specific widgets in your form, which means you can override the way the select renders and put in custom code to check what the value of the option is and output your custom class there.
You need to apply a custom layout to your form, like so
{% form_theme form 'form_theme.html.twig' %}
Then inside the layout file you need to override the specific field for that specific form (unless of course you want to edit the choice_widget directly in which case all fields that use choice will have the functionality).
To do that you have to copy the widget, so choice_widget, then name it [_formName_fieldName_widget]
So if your form name was events and your field name was requireTickets, it'd be _events_requireTickets_widget
The answers that were already provided are very good, and I think #CriticalImpact's is the most flexible. But I just wanted to mention that if you're using a form class, you can also add extra attributes to the field via the form builder definition itself:
class SomeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('someField', "text", array("attr" => array("class" => "someCssClass")))
->add("save", "submit");
}
}
I've found this helpful for basic forms, because it still allows you to make a simple {{ form(someForm) }} call in your Twig file.
(Note that this solution still has the drawback that #CriticalImpact mentioned above)
Add attributes like CSS styles to individual choices can nowadays be achieved with choice_attr, e.g.:
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
// ...
$builder->add('attending', ChoiceType::class, array(
'choices' => array(
'Yes' => true,
'No' => false,
'Maybe' => null,
),
'choice_attr' => function($val, $key, $index) {
// adds a class like attending_yes, attending_no, etc
return ['class' => 'attending_'.strtolower($key)];
},
));

Magento: add icons to the menu-links in your Account-dashboard

Is it possible to add icons to all/some of the menu-links in your account-dashboard? Is there a node/style-attribute in the layout XML-file that should come with the addLink-action?
<action method="addLink" translate="label" module="randomname"><name>randomname</name><path>randomname/index/credits</path><label>Credits</label></action>
You have your default Account Dashboard, Account Information, Addresses, My Orders,... menu-items, but I added a new one; "Credits", and I want to make it "pop" out with an icon and/or another background-color. Couldn't figure out how to do it so far.
Thanks!
EDIT:
Ok, I've found out that there's no parameter to set a css class or id in the addLink() function:
public function addLink($name, $path, $label, $urlParams=array())
{
$this->_links[$name] = new Varien_Object(array(
'name' => $name,
'path' => $path,
'label' => $label,
'url' => $this->getUrl($path, $urlParams),
));
return $this;
}
Now you have two options to add icons to the links. 1. Overwrite the Mage_Customer_Block_Account_Navigation Block Class within your own module and extend the addLink method or 2. you could set the css class/id via jQuery. Good Luck!

Resources