Undefined array key "relation_type" - laravel-backpack

Im having some issues with a backpack project im doing.
What im trying to do change the way the PermissionsManager displays the check boxes when editing a user.
I have managed to create the custom CRUD files and i know that these files are working as i can edit some of the basic fields to add additional user fields.
The code im using to add the relationship is..
$this->crud->addField(
[ // relationship
'name' => 'permission', // the method on your model that defines the relationship
'type' => "relationship",
// OPTIONALS:
// 'label' => "Category",
// 'attribute' => "title", // attribute on model that is shown to user
// 'placeholder' => "Select a category", // placeholder for the select2 input
]
);
The error i get then i try to access the page is..
Undefined array key "relation_type"
Any ideas on what might be causing this?
thanks!

I think you are getting that error because the relationship name is permissions (plural) and not permission (singular).
Let me know if that's the case.
Cheers

Related

Compound fields in search

I'm trying to get first name and last name from a related table
I've tried using an accessor on the related table which works fine when loading the page and for adding/editing but when searching it shows an error
Column not found: 1054 Unknown column 'FullName' in 'where clause'
This column obviously does not exist.
So I have this in my related model
{
return $this->attributes['first_name'] . ' ' . $this->attributes['last_name'];
}
And under the Backpack crud Controller I have
$this->crud->addColumn([ // n-n relationship (with pivot table)
'label' => 'Account', // Table column heading
'type' => 'select',
'name' => 'user_id', // the method that defines the relationship in your Model
'entity' => 'user', // the method that defines the relationship in your Model
'attribute' => 'FullName', // foreign key attribute that is shown to user
'model' => "App\Models\BackpackUser", // foreign key model
]);
Where am I making a mistake, input would be very much appreciated.
That needs a real column.
You can use a view (like you already do) or a model_function column to format the table cell value in your model class - https://backpackforlaravel.com/docs/3.5/crud-columns#model_function
Just see what's simpler for you.
Note: if ordering or searching doesn't work as expected see https://backpackforlaravel.com/docs/3.5/crud-columns#custom-search-logic-for-columns and https://backpackforlaravel.com/docs/3.5/crud-columns#custom-order-logic-for-columns

How to use/link sys_category field in custom model in TYPO3

I am developing an extension in which I am uploading files and for each file upload I need to have one or more categories associated with it.
I have built a custom category model and it shows fine at the backend when creating a record, but I want to show/link the sys_category records instead of my own custom categories.
How do I link that field in my custom model?
If anyone else stumbles upon this, I found the solution from the documentation thanks to #larry-pete.
Simply add these lines to your ext_tables.php file in your extension folder.
// Add an extra categories selection field to the pages table
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::makeCategorizable(
'ext_key',
'your_table_name',
'categories',
array(
// Set a custom label
'label' => 'LLL:EXT:ext_key/Resources/Private/Language/locallang.xlf:additional_categories',
// This field should not be an exclude-field
'exclude' => FALSE,
// Override generic configuration, e.g. sort by title rather than by sorting
'fieldConfiguration' => array(
'foreign_table_where' => ' AND sys_category.sys_language_uid IN (-1, 0) ORDER BY sys_category.title ASC',
),
// string (keyword), see TCA reference for details
'l10n_mode' => 'exclude',
// list of keywords, see TCA reference for details
'l10n_display' => 'hideDiff',
)
);
Hope it helps someone.

Edit form dynamically through subscriber

// buildForm
...
->add('book', 'entity', [
'class' => 'MyBundle\Entity\Book',
'choices' => [],
])
->addEventSubscriber(new MySubscriber());
The field book gets filled through javascript and gets the title of the book.
What I need to do is check if the book already exists in my db, otherwise I create it. I created a subscriber for that works well.
The problem is that I couldn't get rid of the error emitted by $form->handleRequest($request)->isValid(), Which is weird because I edited data in the request this way in my subscriber:
public function preSetData(FormEvent $event)
{
...
$author = $event->getData();
$requestForm = $this->request->request->get('mybundle_author');
$bookTitle = $requestForm['book'];
// if this book title doesn't exist -> create it
...
$requestForm['book] = (string) $book->getId();
$this->request->request->set('mybundle_author', $requestForm);
}
No matter what FormEvents I used, it emits the error that book value is not valid
I crossed a similar problem with the entity type.
The problem is that the new Entity is not marked as managed, and the entity type is focused on selecting existing entities. You could either pass the ObjectManager to the subscriber and set the entity as managed (with persist), or get rid of the validation error yourself. The latter is cleaner, but may require more work.
Removing the option choices fixed the problem.
My subscriber is correct but in my form I had to edit the field
// buildForm
...
->add('book', 'entity', [
'class' => 'MyBundle\Entity\Book',
//'choices' => [], // removing this fixed the problem
])
->addEventSubscriber(new MySubscriber());

Symfony2 entityaudit: Add field on revision table

I have 2 entities in my bundle that are audited via simplethings/entity-audit.
I would like to add a field to REVISIONS table call "reason". Every time a user updates or delete an entity, he/she needs to especicify an reason for doing that (why updating/deleting) via form, and this reason should be associated to the entity revision.
How would you guys do it? I dont have much experience in OOP.
Thank you very much in advance.
for adding field you need to add field in your database like 'ip' next you change your bundle in file "logRevisionsListener.php"
private function getRevisionId()
{
if ($this->revisionId === null) {
$this->conn->insert($this->config->getRevisionTableName(), array(
'timestamp' => date_create('now'),
'username' => $this->config->getCurrentUsername(),
'ip' => $this->config->getCurrentUsername(),(not correct just for test it give me the user name)
), array(
Type::DATETIME,
Type::STRING,
Type::STRING
));
.
.
}
I added here the ip field and change your Revision.php file by adding your field with the getter method

How to get the value of the form field in drupal 7?

I have added a field on the page_node_form with the following code.
switch($form_id){
case 'page_node_form':
$form['comment_popup'] = array(
'#type' => 'select',
'#title' => t('Comments popup'),
'#options' => array('On' => 'On', 'Off' => 'Off'),
);
}
As the comment form appears on the node so I want when the comment_popup field has the value Off then the subject field should not be displayed in the comment form and when the comment_popup field has the value 'On' then the subject field should be displayed.
I have tried the following code but it did not work.
case 'comment_node_page_form':
if($form_state['comment_popup']['#value'] == 'Off') {
$form['subject']['#access']=FALSE;
}
Any one who can help me?
What you're doing can't work I'm afraid, you're setting an element in one form and then trying to read it from another form which doesn't make sense.
What you need to do is add a submit handler to the page_node_form function which saves the comment_popup value for that node to a custom table. Then in the form alter for `comment_node_page_form' you need to read in the comment_popup data for that particular node, and make the access decisions to the form elements based on that.
Hope that helps