Set sys_language_uid in an extbase command controller - typo3

I have a command controller (an importer) run by scheduler, which should persist data as such:
foreach ($items as $item){
// store it
$entry = $this->objectManager->get('STUBR\Importer\Domain\Model\Item');
$entry->setTitle($item['Title']);
$entry->setData(json_encode($item));
// manually set the storage page (defined in scheduler form)
$entry->setPid($itemStoragePid); // works
// manually set the language (defined in scheduler form)
// EDIT
// $entry->setSysLanguageUid = -1; // had typo
$entry->set_languageUid(-1); // works
// END EDIT
$this->itemRepository->add($entry);
}
While neither pid nor sys_language_uid are set in the model, setPid behaves as expected, but setSysLanguageUid does not.
I'm aware that something is or was not quite right with setSysLanguageUid() in extbase (https://forge.typo3.org/issues/45873), although I wasn't able to grasp the problem entirely.
How do I manually store into the sys_language_uid column?
PS: I've tried Extbase Model: setSysLanguageUid not working but probably I missed something

It's solved, I had a typo above
Solution from #Chi at https://stackoverflow.com/a/33798615/160968
/**
* languageUid
* #var int
*/
protected $languageUid;
/**
* #param int $languageUid
* #return void
*/
public function setLanguageUid($languageUid) {
$this->languageUid = $languageUid;
}
/**
* #return int
*/
public function getLanguageUid() {
return $this->languageUid;
}
And then $entry->setLanguageUid(-1);

Related

TYPO3 tx_form conditional skip steps

I using TYPO3 8 LTS and i want to extend the form.
Right now I am trying to add a condition in my form that skips all other steps and runs my finishers. In the documentation it is written that you have to use the afterInitializeCurrentPage function:
/**
* #param FormRuntime $formRuntime
* #param CompositeRenderableInterface $currentPage
* #param null|CompositeRenderableInterface $lastPage
* #param mixed $requestArguments submitted value of the element *before post processing*
* #return CompositeRenderableInterface
*/
public function afterInitializeCurrentPage(
FormRuntime $formRuntime,
CompositeRenderableInterface $currentPage,
CompositeRenderableInterface $lastPage = null,
array $requestArguments = []
): CompositeRenderableInterface {
if ($requestArguments['personalized'] === '0') {
// code here ...
}
return $currentPage;
}
My problem is i do not know how i execute the finishers out of this function..
i hope someone can give me a hint or something else..
[EDIT]
next problem is if i use the afterInitializeCurrentPage method i get an exception for other forms in my site:
Argument 2 passed to VENDOR\YourNamespace\YourClass::afterInitializeCurrentPage() must implement interface TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface, null given, called in [..]/typo3/sysext/form/Classes/Domain/Runtime/FormRuntime.php on line 254
Many Thanks!
You can call the finisher Class Like below.
You need to add below line in ext_localconf.php file. like this
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterInitializeCurrentPage'][]
= \VENDOR\YourNamespace\Hooks\YourClass::class;
After added this function like below on path Classes/Hooks/YourClass.php.
<?php
namespace \VENDOR\YourNamespace\Hooks;
class YourClass
{
/**
* #param \TYPO3\CMS\Form\Domain\Runtime\FormRuntime $formRuntime
* #param \TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface $currentPage
* #param null|\TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface $lastPage
* #param mixed $elementValue submitted value of the element *before post processing*
* #return \TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface
*/
public function afterInitializeCurrentPage(\TYPO3\CMS\Form\Domain\Runtime\FormRuntime $formRuntime, \TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface $currentPage, \TYPO3\CMS\Form\Domain\Model\Renderable\CompositeRenderableInterface $lastPage = null, array $requestArguments = []): CompositeRenderableInterface
{
return $currentPage;
}
}

TYPO3 extension: Prevent record to be saved if the user enters the wrong value

I want to implement an eval in TYPO3 for a specific field in a TCA that prevents the record to be saved in the backend if the BE user enters a restricted value (like if doesn't enter anything or the input contains specific characters). So far i can only change the value in the eval function if it's not allowed, but i want the record NOT to be saved if the BE user clicks on 'save' while the field has restricted value. Is this even possible via an eval?
Edit: A cheap way to do this would be setting $value to NULL in the eval function if the input is of a restricted value, but that's definitely not the elegant way to do things as it throws an SQL error that might confuse the BE user.
So i basically need a way to prevent TYPO3 to persist the repository... Or to set the record back to the state it was in before the BE user clicked on 'save'...
Edit2: This is what i have... Nothing to exciting, just an IPv4 evaluation. But again, it changes only the value to something else, it DOESN'T prevent the creation or editing of the record if the input was not an IPv4.
<?php
namespace Cjk\Icingaconfgen\Evaluation;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class for field value validation/evaluation to be used in 'eval' of TCA
*/
class IPv4Evaluation
{
/**
* #param string $value
* #param string $is_in
* #param bool $set
* #return string
*/
public function evaluateFieldValue($value, $is_in, &$set)
{
if (!filter_var($value, FILTER_VALIDATE_IP)){
$value = 'Fehlerhafte Eingabe (IPv4)';
/** #var FlashMessage $message */
$message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
'Fehlerhafte Eingabe: .conf Datei wird nicht erstellt/editiert. Neue services können nicht hinzugefügt oder editiert werden.',
\TYPO3\CMS\Core\Messaging\FlashMessage::ERROR,
TRUE
);
/** #var $flashMessageService FlashMessageService */
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
$flashMessageService->getMessageQueueByIdentifier()->enqueue($message);
}
return $value;
}
}
So basically, and correct me if i'm wrong, the eval happens BETWEEN the input of the BE user (right after he clicks on the save button) and the persisting of the record. So there has to be a way to prevent the persisting of the new data, not only to change it at will.
I hope this makes my question more clear now, i don't know what else to write to explain it.
I show you a example of a validation in TCA. If validation failed record can't saved.
File myextension/Classes/Validation/Validator/MinReferencesValidator.php
<?php
namespace Vendor\Myextension\Validation\Validator;
/**
* Validator for min references
*/
class MinReferencesValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
/**
* #var array
*/
protected $supportedOptions = [
'min' => [0, 'The minimum references to accept', 'integer'],
];
/**
* The given value is valid if it contains more then min items
*
* #param mixed $value The value that should be validated
* #return void
*/
public function isValid($value) {
if (!is_object($value)) {
$this->addError(
$this->translateErrorMessage(
'LLL:EXT:myextension/Resources/Private/Language/locallang.xlf:validator_object_notvalid',
'myextension'
), 1489870657);
return;
}
$minimum = $this->options['min'];
$countItems = count($value);
if ($countItems < $minimum) {
$this->addError(
$this->translateErrorMessage(
'LLL:EXT: myextension/Resources/Private/Language/locallang.xlf:validator_min_references',
'myextension',
[
$minimum
]
), 1489872300, [$minimum]);
return;
}
}
}
File myextension/Classes/Domain/Model/Youritem.php
/**
* Image of supplier (image reference)
*
* #var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
* #validate NotEmpty, \Vendor\Yourextension\Validation\Validator\MinReferencesValidator(min=1)
*/
protected $images;

Extbase update deleted=1 object in Controller fails (Object with identity x not found)

I want do give the function to 'restore' deleted Object in my FE-Ext. It seems, that it does not find any deleted records an so i cannot update them set deleted = 0.
What you be you sugestion to handle that from the controller?:
$query->getQuerySettings()->setIgnoreEnableFields(TRUE);
$query->getQuerySettings()->setIncludeDeleted(TRUE);
Thank you.
Im not quite sure what you mean by "from the controller". Normally you implement this in your repository and just call the method from the controller.
In your repo:
public function findRecordEvenIfItIsDeleted($uid) {
$query = $this->createQuery();
$settings = $query->getQuerySettings();
settings->setIgnoreEnableFields(TRUE);
settings->setIncludeDeleted(TRUE);
$query->matching($query->equals('uid', $uid));
return $query->execute();
}
In your controller:
$myObject = $this->myRepsository->findRecordEvenIfItIsDeleted($uid);
Done. (Of course your storage pid must be set (or disable respectStoragePage as well)
You're adding does not throw any error because you are setting the querySettings to include deleted records. But maybe, this setting has to be enabled even when you are updating, as the repository should find the object you are updating.
I haven't tested it but give this a try.
In your repository(just a pseudo code)
public function update($modifiedObject) {
settings->setIncludeDeleted(TRUE);
parent::update($modifiedObject);
}
I know this question was asked long time ago but today i had a similar problem with a hidden object. My solution was this one:
add this to your Model (in your case exchange "hidden" by "deleted"):
/**
* #var boolean
*/
protected $hidden;
/**
* #return boolean $hidden
*/
public function getHidden() {
return $this->hidden;
}
/**
* #return boolean $hidden
*/
public function isHidden() {
return $this->getHidden();
}
/**
* #param boolean $hidden
* #return void
*/
public function setHidden($hidden) {
$this->hidden = $hidden;
}
in your repository add this function to find the deleted/hidden object:
public function findHiddenByUid($uid) {
$query = $this->createQuery();
$query->getQuerySettings()->setIgnoreEnableFields(TRUE);
$query->getQuerySettings()->setEnableFieldsToBeIgnored(array('disabled','hidden'));
return $query
->matching($query->equals('uid', $uid))
->execute()
->getFirst();
}
now in your Controller you can read the object, set the "hidden" option and update it:
$yourobject = $this->yourobjectRepository->findHiddenByUid($uid);
$yourobject->setHidden(1);
$this->yourobjectRepository->update($yourobject);
Maybe not interesting for your task but for others:
In my case i additionally had the problem posting a hidden object in a form to an action. So note that if you want to post an object by a form, it is better (or probably necessary) to first set the objects deleted/hidden option to 0.

An error occurred while trying to call Controller->createAction()

I am trying to create something with extbase, but the error-message I get is not very helpful. I took the blog_example extension as a guide. A (maybe) important difference is: I don't have a database table because I want to write a custom domain repository that connects to an external servive through REST.
The actual error message (displayed above the plugin, not as an exception message):
An error occurred while trying to call Tx_MyExt_Controller_SubscriptionController->createAction()
Classes/Controller/SubscriptionController:
Stripped down to the important parts.
class Tx_MyExt_Controller_SubscriptionController extends Tx_Extbase_MVC_Controller_ActionController
{
/**
* #var Tx_MyExt_Domain_Repository_SubscriberRepository
*/
protected $subscriberRepository;
/**
* #return void
*/
public function initializeAction()
{
$this->subscriberRepository = t3lib_div::makeInstance('Tx_MyExt_Domain_Repository_SubscriberRepository');
}
/**
* #param Tx_MyExt_Domain_Model_Subscriber $subscriber
* #dontvalidate $subscriber
* #return string The rendered view
*/
public function newAction(Tx_MyExt_Domain_Model_Subscriber $subscriber = null)
{
$this->view->assign('subscriber', $subscriber);
}
/**
* #param Tx_MyExt_Domain_Model_Subscriber $subscriber
* #return string The rendered view
*/
public function createAction(Tx_MyExt_Domain_Model_Subscriber $subscriber)
{ }
}
Classes/Domain/Model/Subscriber
class Tx_MyExt_Domain_Model_Subscriber extends Tx_Extbase_DomainObject_AbstractEntity
{
/**
* #var string
* #dontvalidate
*/
protected $email = '';
/**
* #param string $email
* #return void
*/
public function setEmail($email)
{
$this->email = $email;
}
/**
* #return string
*/
public function getEmail()
{
return $this->email;
}
}
Resources/Private/Templates/Subscription/new
<f:form action="create" controller="Subscription" objectName="Subscriber" object="{subscriber}" method="post">
<f:form.textfield property="email"></f:form.textfield>
<f:form.submit value="submit"></f:form.submit>
</f:form>
Facts
Adding $subscriber = null removes the message. But $subscriber is null then
A var_dump($this->request->getArguments()); displays the form's fields
There is an index action, and it is also the first action defined in ext_localconf.php
The hints and solutions I found aren't working for me, so I hope someone can guide me into the right direction.
I've got the same bug.
If you pass an Model as argument to an method, it will also validate the model fields.
I've had this annotation on my model property:
/**
*
* #var \string
* #validate NotEmpty
*/
It validates the "#validate" annotation.
The field in the database was empty so i got the error message
An error occurred while trying to call ...
It would be good if there was a better error message.
You need to customize the validation annotation or verify that the property is not empty in the database
Hope it helps somebody
In addtion: check any Validations in your Model and your TCA. If a field is marked as #validate NotEmpty in your Model and is not marked appropriately in the TCA, a record can be saved ignoring the #validate settings in the Model. This can happen if you change the Model and/or TCA after creating records.
An example:
Field 'textfield' is set to not validate, both in the TCA and the Model. You create a new record and save it without filling in the field 'textfield' (you can, it is not set to validate). You then change the Model setting 'textfield' to #validate NotEmpty and then try to show the record on the FE, you will get the error.
The solution for that example:
Simply remove the validation in your Model OR check validations in the TCA and Model so that they work together.
--
A German blog post covers this solution: http://www.constantinmedia.com/2014/04/typo3-extbase-an-error-occurred-while-trying-to-call-anyaction/
just override the template method getErrorFlashMessage in yout controller to provide a custom error message...
/**
* A template method for displaying custom error flash messages, or to
* display no flash message at all on errors. Override this to customize
* the flash message in your action controller.
*
* #return string|boolean The flash message or FALSE if no flash message should be set
* #api
*/
protected function getErrorFlashMessage() {
return 'An error occurred while trying to call ' . get_class($this) . '->' . $this->actionMethodName . '()';
}
classic case of "start over from scratch and it works, and if you compare it you have the same code, though".
I updated the code in the question, maybe it helps someone.

Akeneo 2.1.4 : How can I change a products' parent (used product model)

I have the requirement where upon importing I need to be able to change to products' product model. I tried to do this by changing the parent in the CSV file I'm importing, but this will show the following message:
WARNING
parent: Property "parent" cannot be modified, "new_parent_code" given.
What is the proper way to make this work? I tried 'hacking' the database by manually assigning a different parent to the product by editing the parent directly in the pim_catalog_product-table, and this seemed to work, but when editing the product unexpected results occur.
Could anyone point me in the right direction how I can change a product parent upon importing?
update:
I now came up with the following solution:
In my own bundle, I added Resources/config/updaters.yml (using DependencyInjecten Extension) with the following:
parameters:
# Rewrite parent field setter so we can allow the importer to update the parent:
pim_catalog.updater.setter.parent_field.class: Vendor\Bundle\InstallerBundle\Updater\Setter\ParentFieldSetter
And my custom ParentFieldSetter.php:
namespace Vendor\Bundle\InstallerBundle\Updater\Setter;
use Akeneo\Component\StorageUtils\Exception\ImmutablePropertyException;
use Akeneo\Component\StorageUtils\Repository\IdentifiableObjectRepositoryInterface;
/**
* Class ParentFieldSetter
*/
class ParentFieldSetter extends \Pim\Component\Catalog\Updater\Setter\ParentFieldSetter
{
/**
* #var IdentifiableObjectRepositoryInterface
*/
private $productModelRepository;
/**
* ParentFieldSetter constructor.
* #param IdentifiableObjectRepositoryInterface $productModelRepository
* #param array $supportedFields
*/
public function __construct(
IdentifiableObjectRepositoryInterface $productModelRepository,
array $supportedFields
) {
$this->productModelRepository = $productModelRepository;
parent::__construct($productModelRepository, $supportedFields);
}
/**
* #param \Pim\Component\Catalog\Model\ProductInterface|\Pim\Component\Catalog\Model\ProductModelInterface $product
* #param string $field
* #param mixed $data
* #param array $options
*/
public function setFieldData($product, $field, $data, array $options = []): void
{
try {
parent::setFieldData($product, $field, $data, $options);
} catch (ImmutablePropertyException $exception) {
if ($exception->getPropertyName() === 'parent') {
// Allow us to change the product parent:
if ($parent = $this->productModelRepository->findOneByIdentifier($data)) {
$familyVariant = $parent->getFamilyVariant();
$product->setParent($parent);
$product->setFamilyVariant($familyVariant);
if (null === $product->getFamily()) {
$product->setFamily($familyVariant->getFamily());
}
}
} else {
throw $exception;
}
}
}
}
This works. Now, upon importing the parent gets saved properly. I'm only wondering if:
a). This implementation is correct.
b). I'm not causing some other major issues by changing the parent.
I also noted the following TODO-statement in the original Akeneo-code above the code that throws the error when attempting to change the parent:
// TODO: This is to be removed in PIM-6350.
Anyone from Akeneo care to shed some light on this?