Custom forms in Drupal 8 - forms

I am struggling with some Drupal 8 form/entity type coding.
Basically here is a short summary of the task:
I have entity types which are from the eck module, so say three fields:
Name:
Email:
Phone:
So I have created a custom form using the FormBase class for users to fill in and that's fine but, I don't know how to save the post data to the entity.
If it was a node I would use the Node::create function but this is an entity so I need another way.
So I guess my task is:
1. Get the ID of the entity I want to save too
2. Find a way to save the form data to the entity
I am very new to Drupal 8 (just like most people I guess), I have tried Googling for info but I can't find anything that I understand, any help with this would be amazing, it should be a simple task I would have thought?

I personally don't use the ECK module, so I don't know how it works exactly, I can only imagine. I build my entities by myself.
But what I do know is that the FormBase class is the wrong (not wrong per se, but there's another class which handles exactly what you want) one to use to save entities. You're supposed to use the ContentEntityForm class for that.
Docs: https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!ContentEntityForm.php/class/ContentEntityForm/8.2.x
The FormBase class is the generic form class, you use it basically for custom and simple stuff, where you only need the buildForm, validateForm and submitForm implementations (more or less).

Related

API.AI Intent won't save when adding Required Action

I'm trying to create a chat bot that will help users search up motorcycles.
I'm new to API.AI and have set up my entities and their synonyms, my intent and user expressions, as well as references to the entities (#engineSize, #make, #bikeType).
My problem is when I try to add a required action and prompt, and then try to save the intent, I get the following message:
"The following entities reference each other and form an infinite loop: [engineSize]."
Initially I thought I was using the references wrong in the user expressions. I deleted every reference except for one expression which uses all three entities.
I can't figure out what I'm doing wrong. Any help would be greatly appreciated, thanks! Pix below for further details.
EDIT: I fixed one of the issues of trying to pass a template expression as an example. However, I still get the same error message. I will replace and update my image links to include the edits.
Annotated User expressions
Required Actions
Interestingly enough, the answer to this post would have been difficult to find because the problem was in defining my entities.
In the entity definitions, I included an #ref to the entity itself. ie the bikeType entity contained #bikeType as one of its definitions.
This is not to be mistaken with the User Expressions. As long as the user expression is marked as a Template (the entire line is denoted with an '#' on the far left, as opposed to a large " ), there should be no issues.
Edited for clarity to get at root problem
In the provided user input examples you give the intent, you are supposed to provide general examples and then highlight any text belonging to an entity to map where entities appear in user's inquiries.
In your case, you have input the actual entity reference '#engineSize' as an example belonging to the engineSize entity, creating a self reference.
A proper provided user example would look like:
Also note though that if you are just using entities to store generic information like numbers, addresses, times, etc. it generally makes far more sense to use prebuilt system entities for those categories than create a custom entity, for example #sys.number-integer might be exactly what you need
It looks like you need to get a firmer understanding of entities, for which I would recommend the documentation:
https://docs.api.ai/docs/concept-entities

Symfony - why generate form is not in Type folder

I have a question I ask myself since Symfony 2 and it is still the case on 3.2
When you use the console to generate formType of entity :
doctrine:generate:form
The EntityType is in the \Form folder but not in \Form\Type
Why ?
Sensiolabs itself recommand to put it in the Type folder....
There is a way to adapt this one?
No it doesn't recommend that.
As you can see in either Symfony Doc examples or Symfony Best Practices Guide, it both use Form and not Type folder/namespace.
The second one even straightly says:
Best Practice
Put the form type classes in the AppBundle\Form namespace, unless you use other custom form classes like data transformers.
Namespace of course, means also path at the same time.
In addition, best practices recommends creating a sub namespace if other custom form classes are used (like DataTransformer).
For example, i always use :
---Form
-----------DataTransformer
-----------Type
In this case all form classes are stored in Type folder (and namespace).

Mirror formatting spring-data-rest/spring-hateoas in custom controllers

I used the suggested approach in this question to return HATEOAS formatted outputs that match those returned by spring-data-rest. It works good, but is there a way to avoid boiler plate code to create entity resource assemblers like the QuestionResourceAssembler in the referenced question, if I only want to add 'self' links using the id to all entities? Perhaps using ResourceAssemblerSupport?
The easiest way is to simply use the Resource wrapper type:
Resource<Person> personResource = new Resource<>(person);
personResource.addLink(…);
personResource.addLink(…);
Links can be created either by simply instantiating them (i.e. new Link("http://localhost/foo", "relation") or by using the ControllerLinkBuilder which allows you to point to Controller methods for obtain a reverse mapping. See this section of the Readme for details.

django-rest-framework - autogenerate form in browsable API?

Not sure if i'm using the right vocabulary. In the browsable api that comes for free with django-rest-framework, I was wondering if there was a way to autogenerate a form similar to how we define ModelForms. This would allow us to more easily test input to the API in some cases.
I'm currently using ModelSerializers and the generic view APIView in case that makes a difference.
I have read the documentation (several times at this point) but didn't see it mentioned anywhere.
If you're using the generic class-based-views you'll get that for free. Try the live tutorial at http://restframework.herokuapp.com logging in as one of the users, so that you can create some snippets. eg user: 'max', password: 'max'.
Any views subclassing GenericAPIView and setting a serializer_class will get that behavior, as REST framework can determine what the form should look like.
For example:
(Note the form input at the bottom of the screen shot)
If you're just working from APIView you'll get the generic content input (such as json), like the once you've included a screenshot of, which is also useful, but not quite as convenient as the forms.
Create a serialiser class that fits the form input fields you want and set it on your APIView like so;
class MyView(APIView):
serializer_class = MySerializer # Used for the form in the browsable api
That works just perfectly.
Example of a serializer class based on a model:
from rest_framework import serializers
class MySerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
class MyApiView(APIView):
"""My Demo API View"""
serializer_class = serializers.MySerializers
Make sure you're using the name "serializer_class" and not any other name like serializers_class.
using the exact "serializer_class" will autogenerate form in the browseable API

using kohana zend lucene search

i have a kohana-based website, and i want to use zend-lucene search.
as i have seen in the documentation here https://github.com/evopix/kohana-search/blob/master/README.markdown but i do not understand: must i re-create the models i already have in order to be able to use it? the model should extend the ORM_Searchable (it is a must)? and where can i find the ORM_Searchable class in kohana?
thanks a lot!
You don't have to recreate the models. You extend from a new class, implement its methods which specify what data you want to be indexed by Zend Lucene. The documentation is really straight forward.
the model should extend the ORM_Searchable (it is a must)? and where can i find the ORM_Searchable class in kohana?
This is in the code repository you linked to. classes/kohana/orm/searchable.php