Shopware 6 Plugin - text snippet not deleted on uninstall plugin - order custom field label - custom-fields

We created custom fields within our plugin for orders and for products.
Shopware creates text snippets for the custom fields labels.
These should be removed when uninstalling the plugin.
It works for the products custom field.
...
'customFields' => [
[
'name' => 'product_custom_field_name_dummy',
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => 'checkbox',
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'label' => [
self::GER_ISO => 'Label GER',
self::EN_ISO => 'Label EN',
Defaults::LANGUAGE_SYSTEM => 'Label EN',
]
],
]
],
'relations' => [
[
'entityName' => ProductDefinition::ENTITY_NAME,
],
],
...
But not for the orders custom fields.
...
'customFields' => [
[
'name' => 'order_custom_field_name_dummy_one',
'type' => CustomFieldTypes::TEXT,
'config' => [
'customFieldType' => CustomFieldTypes::TEXT,
'label' => [
self::GER_ISO => 'Order Label GER',
self::EN_ISO => 'Order Label EN',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN',
]
],
],
[
'name' => 'order_custom_field_name_dummy_two',
'type' => CustomFieldTypes::SELECT,
'config' => [
'customFieldType' => CustomFieldTypes::SELECT,
'componentName' => 'sw-single-select',
'label' => [
self::GER_ISO => 'Order Label GER 2',
self::EN_ISO => 'Order Label EN 2',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN 2',
],
'options' => [
...
]
]
],
[
'name' => 'order_custom_field_name_dummy_three',
'type' => CustomFieldTypes::DATETIME,
'config' => [
'customFieldType' => CustomFieldTypes::DATETIME,
'label' => [
self::GER_ISO => 'Order Label GER 3',
self::EN_ISO => 'Order Label EN 3',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN 3',
]
],
],
[
'name' => 'order_custom_field_name_dummy_four',
'type' => CustomFieldTypes::SELECT,
'config' => [
'customFieldType' => CustomFieldTypes::SELECT,
'componentName' => 'sw-single-select',
'label' => [
self::GER_ISO => 'Order Label GER 4',
self::EN_ISO => 'Order Label EN 4',
Defaults::LANGUAGE_SYSTEM => 'Order Label EN 4',
],
'options' => [
...
]
],
],
],
'relations' => [
[
'entityName' => OrderDefinition::ENTITY_NAME,
],
],
...
Is this a problem Shopware has with order custom fields or did we possibly make a mistake when creating the order custom fields?
Edit:
The custom fields are created on install method und removed on uninstall method inside the plugin via the CustomFieldSetRepository.
Edit:
This is how we delete the custom fields on uninstall:
public function uninstallCustomFieldSet() {
$customFieldSet = $this->getCustomFieldSet(self::CUSTOM_FIELD_SET_NAME);
if ($customFieldSet instanceof CustomFieldSetEntity) {
$this->customFieldSetRepository->delete([['id' => $customFieldSet->getId()]], $this->context);
}
}
protected function getCustomFieldSet(string $customFieldSetName): ?CustomFieldSetEntity {
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $customFieldSetName));
$criteria->addAssociation('customFields');
$criteria->addAssociation('relations');
$customFieldSet = $this->customFieldSetRepository->search($criteria, $this->context)->first();
if ($customFieldSet instanceof CustomFieldSetEntity) {
return $customFieldSet;
} else {
return null;
}
}

The author mentions that Shopware 6 confirms that it's a bug.
I am creating an answer to this question even though it was already answered in the comments so that it can be accepted & this question resolved.

Related

How to get the selected option of a radion button element in Zend Framework 2?

In a Fieldset I have an Element\Radio foo and Element\Text bar.
public function init()
{
$this->add(
[
'type' => 'radio',
'name' => 'foo',
'options' => [
'label' => _('foo'),
'value_options' => [
[
'value' => 'a',
'label' => 'a',
'selected' => true
],
[
'value' => 'b',
'label' => 'b'
]
]
]
...
]);
$this->add(
[
'name' => 'bar',
'type' => 'text',
'options' => [
'label' => 'bar',
...
],
...
]);
}
The validation of the field bar is depending on the selected foo option. It's easy to implement, if I can get the selected value of foo:
public function getInputFilterSpecification()
{
return [
'bar' => [
'required' => $this->get('foo')->getCheckedValue() === 'a',
...
],
];
}
But there is no method Radio#getCheckedValue(). Well, I can iterate over the $this->get('foo')->getOptions()['value_options'], but is it really the only way?
How to get (in the Fieldset#getInputFilterSpecification()) the selected option of a Zend\Form\Element\Radio?
The selected option gets POSTed to the server along with everything else from the HTML form and is all of this is available in validators through the $context array.
You can create a conditionally required field by using a callback validator and the $context array like this:
public function getInputFilterSpecification() {
return [
'bar' => [
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
'required' => true,
'validators' => [
[
'name' => 'Callback',
'options' => [
'callback' => function ($value, $context) {
return $context['foo'] === 'a'
},
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'This value is required when selecting "a".'
]
]
]
]
],
];
}
That would check if 'foo' is equal to 'a', i.e. option 'a' is selected and return true when it is, which marks the input as valid, and false when it's not, marking the input invalid.

How to enable displaying the global label unsing FormMultiCheckbox in ZF2?

I'm using Zend\Form\Element\MultiCheckbox with Zend\Form\View\Helper\FormMultiCheckbox:
MyFieldset.php
// namespace ...;
// use ....;
class MyFieldset extends Fieldset
{
// ...
public function init()
{
parent::init();
$this->add(
[
'type' => 'multi_checkbox',
'name' => 'mymulticheckbox',
'options' => [
'label' => _('global label'),
'label_attributes' => [
'class' => 'col-md-3',
],
'value_options' => [
[
'value' => 'foo',
'label' => 'FOO',
],
[
'value' => 'bar',
'label' => 'BAR',
],
[
'value' => 'buz',
'label' => 'BUZ',
],
]
],
]
);
}
// ...
}
myform.phml
use Zend\Form\View\Helper\FormMultiCheckbox;
echo $this->formMultiCheckbox($myFieldset->get('mymulticheckbox'), FormMultiCheckbox::LABEL_PREPEND);
It works, but the "global label" is not displayed. It gets displayed, when I'm using Zend\Form\View\Helper\FormElement, but the FormMultiCheckbox seems to ignore the "global label".
How to make FormMultiCheckbox display the label of the checkbox list?
Have you tried with formRow(). For me it works. This does not appear to be managed in formMultiCheckbox(). See lines 182-193, file zend-form/src/View/Helper/FormRow.php.
// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
// labels. The semantic way is to group them inside a fieldset
if ($type === 'multi_checkbox'
|| $type === 'radio'
|| $element instanceof MonthSelect
|| $element instanceof Captcha
) {
$markup = sprintf(
'<fieldset><legend>%s</legend>%s</fieldset>',
$label,
$elementString
);

Configure select2 API to hide toggle

How do I hide select all on select2?
https://select2.github.io/options.html
http://demos.krajee.com/widget-details/select2
'showToggleAll' => false, // does not work
you might want to find out why it does not work sense the demo version works ok but if you need to hide it...
$(".s2-togall-button, .s2-togall-select").css("display", "none")
run after the select2 has loaded seem to work for me.
Correct use of that option is like next:
echo $form->field($model, 'attribute')->widget(
Select2::classname(),
[
'data' => $arrayValues,
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'class' => 'form-control',
'prompt'=> 'Select up to 3 values',
'multiple' => true,
],
'pluginOptions' => [
'tags' => false,
'maximumSelectionLength' => 3,
'allowClear' => false,
//'tokenSeparators' => [',', ' '],
'closeOnSelect' => true,
],
'showToggleAll' => false,
]
);
I hope will be useful

How to validate empty input in Zend Framework 2

I'm doing a registration form in ZF2, but I don't get how to validate. Validation seems not working.
I'm adding the validators array, but it doesn't work anyways. I don't know how can I fix that.
This is my code of controller:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Form\Formularios;
use Zend\Db\Adapter\Adapter;
use Application\Modelo\Entity\Usuarios;
class FormularioController extends AbstractActionController
{
public $dbAdapter;
public function indexAction()
{
return new ViewModel();
}
public function registroAction()
{
if($this->getRequest()->isPost())
{
$this->dbAdapter=$this->getServiceLocator()->get('Zend\Db\Adapter');
$u=new Usuarios($this->dbAdapter);
//echo "se recibiĆ³ el post";exit;
$data = $this->request->getPost();
$u->addUsuario($data);
return $this->redirect()->toUrl($this->getRequest()->getBaseUrl().'/application/formulario/registro/1');
}else
{
//zona del formulario
$form=new Formularios("form");
$id = (int) $this->params()->fromRoute('id', 0);
$valores=array
(
"titulo"=>"Registro de Usuario",
"form"=>$form,
'url'=>$this->getRequest()->getBaseUrl(),
'id'=>$id
);
return new ViewModel($valores);
}
}
}
this is my form code with validator
class Formularios extends Form
{
public function __construct($name = null)
{
parent::__construct($name);
$this->add(array(
'name' => 'name',
'required' => true,
'allow_empty' => false,
'options' => array(
'label' => 'Nombre Completo',
),
'attributes' => array(
'type' => 'text',
'class' => 'input'
),
'filters' => [ ['name' => 'StringTrim'], ],
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Ingrese Nombres.',
))))
));
$this->add(array(
'name' => 'lastname',
'required' => true,
'options' => array(
'label' => 'Apellido',
),
'attributes' => array(
'type' => 'text',
'class' => 'input'
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Ingrese Apellidos.',
))))
));
Thanks in advance
First problem.
$data = $this->request->getPost(); should be $data = $this->getRequest()->getPost();
Second problem is that you call your validators direclty when you build your form in the view, which is wrong. The right way to do is via an inputFilter. Now, there are many ways to to this, for example: with or without a factory called from your model or via the for class with a form element manager
I will show you the model way with a factory since it's easier for new comers.
namespace MyModule\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class MyModel implements InputFilterAwareInterface
{
/**
* #var null $_inputFilter inputFilter
*/
private $_inputFilter = null;
// some more code like exhnageArray get/set method
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add(
$factory->createInput([
'name' => 'id',
'required' => false,
'filters' => [
['name' => 'Int'],
],
])
);
$inputFilter->add(
$factory->createInput([
"name"=>"title",
"required" => true,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
['name' => 'NotEmpty'],
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 200,
],
],
],
])
);
$inputFilter->add(
$factory->createInput([
"name"=>"text",
"required" => true,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
['name' => 'NotEmpty'],
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
],
],
],
])
);
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
Third proble. DO NOT EVER use serviceManager in controller. It's a really really really bad practice. Instead use a factory.

How can i add parameter in mandrill html text?

Is there any way to include the parameter i html portion of mandrill ?
I am not using template.
here is my code.
$mandrill = new Mandrill('YOUR_API_KEY');
$message = array(
'html' => '<p>here is my email</p>',
'text' => 'Example text content',
'subject' => 'example subject',
'from_email' => 'message.from_email#example.com',
'from_name' => 'Example Name',
'to' => array(
array(
'email' => 'recipient.email#example.com',
'name' => 'Recipient Name',
'type' => 'to'
),
array(
'email' => 'recipient.email#example.com',
'name' => 'Recipient Name',
'type' => 'to'
)
),
What i want to do is to add a parameter to the html content.
'html' => '<p>here is my email{ param}</p>', // this line
How can i do this ?
Thanks
Sounds like you're looking to use Mandrill merge tags.
You can insert merge tags into your HTML content and have it inject personalized information for each recipient. They can be used in your content with the format *|MERGETAG|* and by defining "merge_vars" in your message array.
Here's an example from their docs:
"message": {
"global_merge_vars": [
{
"name": "var1",
"content": "Global Value 1"
}
],
"merge_vars": [
{
"rcpt": "emailadress#domain.com",
"vars": [
{
"name": "fname",
"content": "John"
},
{
"name": "lname",
"content": "Smith"
}
]
}
],
source:
http://help.mandrill.com/entries/21678522-How-do-I-use-merge-tags-to-add-dynamic-content-
In testing a bit, Mandrill actually supports a few merge tags "out of the box", so you should just be able to add *|EMAIL|* into your HTML content and it'll pull that email address in from your TO array.

Resources