Using Page Type Attribute on form
Permalink 1 user found helpful
Hi all!
I am creating a website where the client wants people to be able to apply for a job online. I want to include a basic form (name, email, file upload for CV/Resume, Supporting Statement) at the bottom of the page.
I am using the composer for my client to be able to upload the job details which includes a job title and reference number. I want to be able to automatically include these in the form when someone applies for the job.
I am suffering from brain freeze and cannot think how to (easily) include these custom attributes on the form. The attributes are 'job_reference' and 'job_title'.
Thank you in advance
Simon
I am creating a website where the client wants people to be able to apply for a job online. I want to include a basic form (name, email, file upload for CV/Resume, Supporting Statement) at the bottom of the page.
I am using the composer for my client to be able to upload the job details which includes a job title and reference number. I want to be able to automatically include these in the form when someone applies for the job.
I am suffering from brain freeze and cannot think how to (easily) include these custom attributes on the form. The attributes are 'job_reference' and 'job_title'.
Thank you in advance
Simon
There is not a good way to do this using the standard form block, if each form is going to be exactly the same except for your page attributes perhaps you should consider using an External Form instead of the standard Form block.
In our eCommerce package Razor Commerce we use attributes (which we call Fields) for the checkout and in other places. So for example Billing Address is an Address Attribute. Saves us outputting all the different address fields.
I definitely encourage you to try to make this method work rather than resorting to something else because it very achievable and once you learn it you'll be able to output any attribute you want, anytime. It doesn't make sense to me to use attributes partly, then resort to other forms unless the attribute output is really unsuitable to what you need.
Here is our Element for the checkout pane, we abstract all this into a Field API so all you'll see here is our call to method Render: https://github.com/RazorCommerce/razor-commerce/blob/development/raz...
Now take a look at the Field API method render() https://github.com/RazorCommerce/razor-commerce/blob/development/raz...
Notice in render() we just determine if it's a User or Collection attribute first, then use the appropriate class. Key point is you need to use a different class for each AttributeKeyType. For Collection Attributes, see method renderCollectionField().
You can almost get by just with the part of that method below but you may need other parts to get it working:
Bottom line is every AttributeKey has that method "render" and when you pass "form" along with the value it outputs the form and the value if the value is set. This code example incomplete though because you'll need to load the $page in order to get the value. And if it's a UserAttribute, then same idea but you'll need the User to load the value.
I definitely encourage you to try to make this method work rather than resorting to something else because it very achievable and once you learn it you'll be able to output any attribute you want, anytime. It doesn't make sense to me to use attributes partly, then resort to other forms unless the attribute output is really unsuitable to what you need.
Here is our Element for the checkout pane, we abstract all this into a Field API so all you'll see here is our call to method Render: https://github.com/RazorCommerce/razor-commerce/blob/development/raz...
Now take a look at the Field API method render() https://github.com/RazorCommerce/razor-commerce/blob/development/raz...
Notice in render() we just determine if it's a User or Collection attribute first, then use the appropriate class. Key point is you need to use a different class for each AttributeKeyType. For Collection Attributes, see method renderCollectionField().
You can almost get by just with the part of that method below but you may need other parts to get it working:
$field = AttributeCollectionKey::getByHandle( $handle ); $fieldValue = $page->getAttributeValueObject( $field ); print $field->render('form', $fieldValue, true);
Bottom line is every AttributeKey has that method "render" and when you pass "form" along with the value it outputs the form and the value if the value is set. This code example incomplete though because you'll need to load the $page in order to get the value. And if it's a UserAttribute, then same idea but you'll need the User to load the value.
Note that if you're copying stuff from https://github.com/RazorCommerce/razor-commerce/blob/development/raz... check the use statements because for instance there is no AttributeCollectionKey, we use Concrete\Core\Attribute\Key\CollectionKey as AttributeCollectionKey.
Also in the docs, if you want to look closer at the Attribute Render method it's inherited from Key\Key as in http://concrete5.org/api/class-Concrete.Core.Attribute.Key.Key.html...
Also in the docs, if you want to look closer at the Attribute Render method it's inherited from Key\Key as in http://concrete5.org/api/class-Concrete.Core.Attribute.Key.Key.html...