Add custom attribute in magento 2 - magento2

How to add a custom field to the frontend registration form.
I already added required feilds in database by following
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'sample_attribute',
[
'type' => 'varchar',
'label' => 'Sample Attribute',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]
);
$sampleAttribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'sample_attribute');
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
$sampleAttribute->setData(
'used_in_forms',
['adminhtml_customer']
);
$sampleAttribute->save();
}
my feild listed in magento admin. I'm unable to display it in frontend

The path of customer_account_create.xml should be Vendor_Module\view\frontend\layout\.
For those who are new to Magento and do not know about all the filename and filepath conventions to be precise is very important to avoid spending time searching for errors.

first thing that comes to my eyes is that attributes are created just for the backend, if you set
'used_in_forms' => ['adminhtml_customer']
only, it will display only in backend. You at least have to add
'used_in_forms' => ['adminhtml_customer',
'customer_account_edit','customer_address_edit','customer_register_address']
but you should post also the code of your register form in frontend

To add additional field in a customer account
Vendor_Module\layout\customer_account_create.xml
<?xml version="1.0"?><page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Framework\View\Element\Template" name="my_form_additional_info_customer" template="Vendor_Module::additionalinfocustomer.phtml"/>
</referenceContainer>
</body>
</page>
Vendor_Module\view\frontend\templates\additionalinfocustomer.phtml
<fieldset class="fieldset create account" data-hasrequired="<?php /* #escapeNotVerified */echo __('* Required Fields') ?>">
<legend class="legend"><span><?php /* #escapeNotVerified */ echo __('Additional Information') ?></span></legend>
<p>
<div class="field regulation required">
<label for="Mobile" class="label">
<span><?php /* #escapeNotVerified */ echo __('Mobile Number') ?></span>
</label>
<div class="control">
<input type="text" name="mobile_number" id="mobile_number" title="<?php /* #escapeNotVerified */ echo __('Mobile Number') ?>" class="input-text" data-validate="{required:true}">
</div>
</div>
</p>
</fieldset>
Vendor_Module\Setup\InstallData.php
<?php
/**
* Copyright © 2018 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* Install data
* #codeCoverageIgnore
*/
class InstallData implements InstallDataInterface {
/**
* CustomerSetupFactory
* #var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* $attributeSetFactory
* #var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* initiate object
* #param CustomerSetupFactory $customerSetupFactory
* #param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
/**
* install data method
* #param ModuleDataSetupInterface $setup
* #param ModuleContextInterface $context
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
/** #var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
/** #var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
/**
* customer registration form default field mobile number
*/
$customerSetup->addAttribute(Customer::ENTITY, 'mobile_number', [
'type' => 'varchar',
'label' => 'Mobile Number',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'sort_order' => 1000,
'position' => 1000,
'system' => 0,
]);
//add attribute to attribute set
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile_number')
->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer', 'customer_account_create', 'customer_address_edit', 'customer_register_address', 'checkout_register', 'adminhtml_checkout'],
]);
$attribute->save();
}
}
After that run Below Commands:
php bin/magento setup:upgrade
php bin/magento cache:flush

Related

TYPO3 FileReference does not save the tablename on the DB. Uploading file from frontend on TYPO3

In my custom extension on TYPO3 10.4 I'm trying to upload a file (image) from the frontend. The file gets uploaded just fine, the rows on the DB seemed to be inserted just fine but there is some data missing.
This is my form:
<f:form method="post" action="create" name="blackboard"
object="{blackboard}" enctype="multipart/form-data">
<f:form.textfield placeholder="Titel*" required="true" property="title"></f:form.textfield>
<f:form.upload property="image" name="image" />
<f:form.submit class="btn btn-primary" value="{f:translate(key: 'submit', default: 'Absenden')}"></f:form.submit>
</f:form>
The model:
/**
* image
*
* #var \TYPO3\CMS\Extbase\Domain\Model\FileReference
* #TYPO3\CMS\Extbase\Annotation\ORM\Cascade("remove")
*/
protected $image = null;
/**
* Returns the image
*
* #return \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
*/
public function getImage()
{
return $this->image;
}
/**
* Sets the image
*
* #param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
* #return void
*/
public function setImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image)
{
$this->image = $image;
}
The controller:
/**
* action create
* #param Blackboard
*/
public function createAction(Blackboard $blackboard)
{
$blackboard->setPid($GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['hebo_blackboards']['BlackboardsStoragePId']);
$blackboard->setUser($GLOBALS['TSFE']->fe_user->user['uid']);
$this->blackboardRepository->add($blackboard);
}
Surprisingly, just that easy, this seems to work just fine. I get the image uploaded on the server, the correct UID of that sys_file_reference on my custom table, the sys_file_reference gets the correct UID of that sys_file... but as you can see in the pic that follows there are a few data missing, "tablename" and "table_local" and as soon as I add that data manually the relationships work (the first rows, where this data is not missing is from rows created from the backend, working fine)
My question is, why? What do I do to fix that?
The problem is that extbase doesn't know those values, therefore you need to state those in the TCA. Given this example
'extra_files' => [
'label' => 'A file',
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
'extra_files',
[
'foreign_match_fields' => [
'tablenames' => 'tx_yourtable_domain_model_fo',
'table_local' => 'sys_file'
]
],
$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
),
],
The foreign_match_fields part is the relevant one which is not needed if you don't handle file uploads in the Frontend.

Email Verification Laravel 8

i am using laravel 8 and trying to verify my email at the time of registration
and getting error
Cannot send message without a sender address
as this is error i'm getting
here is my SMTP setting in .env file
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=abc#gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=admin#gmail.com
MAIL_FROM_NAME="${APP_NAME}"
and here is my user model code
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, Notifiable,HasRoles;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
and this is my route from web.php
Auth::routes(['verify' => true]);
Change your port or you can do php artisan config:cache
Go to folder vendor\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php and then change $options = []; to $options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true);
I found the solutions here https://laracasts.com/discuss/channels/laravel/stream-socket-enable-crypto-ssl-operation-failed-with-code-laravel-7. Its work for me.

Troubleshooting CakePHP form submission

I recently set up the ability to tag posts on my site. I had everything working fine. Then as I was wrapping up I tested all my admin side forms again. The Add Tag form no longer does anything. It doesn't even flash an error or redirect after submission. The page just reloads at the same URL. The only changes to the site I have made since initial testing was move the forms to the admin side of the dev site. Here is some code to hopefully reveal what the mystery is. Also my edit tag form is doing similar thing. It has no flash message but redirects back to the index, like its supposed to but with no changes made to the tag. Ill include the edit code as well.
Add.ctp in src/Template/Admin/Tags/Add.ctp
<div class="tags form large-9 medium-8 columns content">
<?= $this->Form->create($tag) ?>
<div class="form-group">
<fieldset>
<h1 class="page-header">New Tag</h1>
<?php
echo $this->Form->input('name', ['class' => 'form-control']);
?>
</fieldset>
</div>
<?= $this->Form->button(__('Submit'), ['class' => 'btn btn-primary']) ?>
<?= $this->Form->end() ?>
</div>
Here is my Add funciton in my TagsController:
public function add()
{
$this->viewBuilder()->layout('admin');
$tag = $this->Tags->newEntity();
if ($this->request->is('post')) {
$tag = $this->Tags->patchEntity($tag, $this->request->data);
if ($this->Tags->save($tag)) {
$this->Flash->success(__('The tag has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The tag could not be saved. Please, try again.'));
}
$this->set(compact('tag'));
$this->set('_serialize', ['tag']);
}
Here is my Edit funciton in my TagsController:
public function edit($id = null)
{
$this->viewBuilder()->layout('admin');
$tag = $this->Tags->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$tag = $this->Tags->patchEntity($tag, $this->request->data);
if ($this->Tags->save($tag)) {
$this->Flash->success(__('The tag has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The tag could not be saved. Please, try again.'));
}
$this->set(compact('tag'));
$this->set('_serialize', ['tag']);
}
Edit.ctp in src/Template/Admin/Tags/Edit.ctp
<div class="tags form large-9 medium-8 columns content">
<?= $this->Form->create($tag) ?>
<div class="form-group">
<fieldset>
<h1 class="page-header">Edit Tag</h1>
<?php
echo $this->Form->input('name', array('class' => 'form-control'));
?>
</fieldset>
</div>
<?= $this->Form->button(__('Submit'), ['class' => 'btn btn-primary']) ?>
<?= $this->Form->end() ?>
</div>
Just as a side note. I started getting errors when creating a new post as well.
General error: 1364 Field 'section_id' doesn't have a default value
I did go into my DB and give the field a default value. But then when I fill out the form for a new post again, the error just moves to the next table column. I am assuming they are some how related since they popped up at the same time and because tags and posts are related to each other.
TagsTable:
class TagsTable extends Table
{
/**
* Initialize method
*
* #param array $config The configuration for the Table.
* #return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('tags');
$this->displayField('name');
$this->primaryKey('id');
$this->hasMany('PostsTags', [
'foreignKey' => 'tag_id'
]);
}
/**
* Default validation rules.
*
* #param \Cake\Validation\Validator $validator Validator instance.
* #return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->requirePresence('name', 'create')
->notEmpty('name');
return $validator;
}
}
Tags Entity:
class Tag extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* #var array
*/
protected $_accessible = [
'*' => false,
'id' => false
];
}
When I place <?php debug($tag); ?> into my add.ctp view this is the out put it gives me:
object(App\Model\Entity\Tag) {
'[new]' => true,
'[accessible]' => [],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Tags'
}
Again, in question always post debug pathEntity output, in your case debug($tag), also Tag Entity, your validation code, and how looks your db tags table.
Answer:
General error: 1364 Field 'section_id' doesn't have a default value
This means that you have not passed a value for this field.
You can change that table field to accept null or empty value and/or set default if not passed from application, or make validation in your TagsTable to be sure if submitted data valid before send to db.
After question updated:
protected $_accessible = [
'*' => false, <---- should be true
'id' => false
];
This means that all fields except id are accessible

How to update images stored as strings in extbase?

I have recently created an extension that has a file upload feature. I decided to store it as a string. I have used it like this:
In the controller:
public function initializeAction() {
if ($this->arguments->hasArgument('blog')) {
$this->arguments->getArgument('blog')->getPropertyMappingConfiguration()->setTargetTypeForSubProperty('image', 'array');
}
}
In the model:
/**
* Returns the image
*
* #return string $image
*/
public function getImage()
{
return $this->image;
}
/**
* Sets the image
*
* #param \array $image
* #return void
*/
public function setImage(array $image)
{
die(debug::var_dump($image));
if (!empty($image['name']))
{
$imageName = $image['name'];
$imageTempName = $image['tmp_name'];
$basicFileUtility = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\BasicFileUtility');
$imageNameNew = $basicFileUtility->getUniqueName($imageName, \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('uploads/tx_myextension/'));
\TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move($imageTempName, $imageNameNew);
$this->image = basename($imageNameNew);
}
}
The TCA:
'config' => [
'type' => 'group',
'internal_type' => 'file',
'uploadfolder' => 'uploads/tx_myextension',
'show_thumbs' => 1,
'size' => 1,
'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
'disallowed' => ''
],
In my form:
<f:form action="update" name="blog" object="{blog}" >
<f:form.upload property="image" class="form-control" />
...
Now this works perfectly when a new object is created, however when I try to change this image (using updateAction), I get the this error message:
Exception while property mapping at property path "image":
No converter found which can be used to convert from "string" to "array".
I would like to avoid uploading via FAL or writing my own conversion. I'm hoping that I just missed something trivial.
Take an look to an update script from tt_address to see how to make an update.
To make it short: You must read all entries of your files and move them from upload dir to your file storage and then you must add an file_reference which connect the sys_file with your domain model:
GeneralUtility::upload_copy_move(
PATH_site . 'uploads/tx_myextension/' . $imageName,
PATH_site . 'fileadmin/tx_myextension/' . $imageName);
$fileObject = $this->storage->getFile('fileadmin/tx_myextension/' . $imageName);
if ($fileObject instanceof \TYPO3\CMS\Core\Resource\File) {
$this->fileRepository->add($fileObject);
$dataArray = [
'uid_local' => $fileObject->getUid(),
'tablenames' => 'tx_your_domain_model',
'fieldname' => 'image',
'uid_foreign' => 'your model uid',
'table_local' => 'sys_file',
'cruser_id' => 999,
'pid' => 'your_model_pid',
'sorting_foreign' => $imageCount,
'sys_language_uid' => 0
];
if ($this->getDatabaseConnection()->exec_INSERTquery('sys_file_reference', $dataArray)) {
$imageCount++;
}
}
It was a very stupid and simple mistake from my part. In the edit form I forgot to add this: enctype="multipart/form-data"
<f:form action="update" enctype="multipart/form-data" name="blog" object="{blog}" >

trying to send email with laravel, get an error. What does this mean?

I'm trying to send email with laravel, get an error. What does this mean?
stream_set_blocking() expects parameter 1 to be resource, null given
Edit: Mail code.
controller:
public function postSubmit(Request $request)
{
Mail::send('emails.contact', ['data' => $request->all()], function ($m) {
$m->from(config('mail.from.address'), config('mail.from.name'));
$m->to('xxxxx', 'xxxx')->subject('Contact Form Submitted');
});
}
Routes:
Route::get('/contact', 'ContactController#index');
Route::post('/contact/submit', 'ContactController#postSubmit');
View:
<form role="form" id="feedbackForm" data-toggle="validator" data-disable="false" method="POST" action="{{ url('contact/submit') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
Send view:
<strong>You have a new feedback from contact page!</strong>
<p><strong>Name: </strong> {{$data['name']}}</p>
<p><strong>Email: </strong> {{$data['email']}}</p>
<p><strong>Message: </strong> {{$data['message']}}</p>
Edit2: disable_functions: "phpinfo, system, exec, passthru, proc_open, shell_exec, popen, setlimit, mysql_pconnect, stream_socket_client"
These functions are disabled by the host. (They won't enable them due security reasons). I've been informed that I can use mail(). How does that work exactly?! (smtp, username, password, etc..)
This is how I managed to get laravel to send an email.
Yes, of course. Here's the solution that I used in order to configure email for laravel.
indexmail.php, root folder
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="your-email#your-domain.com";
$subject="Enquiry!";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email-Id:
$email
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:../contact.php?msg=Successful Submission! Thankyou for contacting us.");
else
header("Location:../contact.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}
?>
routes.php
Route::post('/contact/submit', 'ContactController#postSubmit');
config/mail.php
'from' => ['address' => 'Sender#email.com', 'name' => 'MyName'],
'pretend' => false,
email view.
<strong>You have a new feedback from contact page!</strong>
<p><strong>Name: </strong> {{$data['name']}}</p>
<p><strong>Email: </strong> {{$data['email']}}</p>
<p><strong>Message: </strong> {{$data['message']}}</p>
contact form.
<form role="form" id="feedbackForm" data-toggle="validator" data-disable="false" method="POST"
action="{{ url('contact/submit') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
app/Http/Requests/ContactFormRequest.php
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class ContactFormRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'message' => 'required',
];
}
}
app/User.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Let me know if it works! original refference