hackforwesternmass / lightupthemap

Light Up The Map was started by a group of Western Mass moms who were fed up with the slow pace of climate legislation. Our children's futures and health will not wait. We knew we needed to activate more people and more Representatives, and fast.
https://www.lightupthemap.com/
0 stars 1 forks source link

Integrate representative API #4

Closed a-pasquale closed 7 years ago

a-pasquale commented 7 years ago

Users entering an address should pull representatives from API with name, photo, phone number, and email.

rick02840 commented 7 years ago

More description of the issue: Currently the user is required to find out who their state rep and senator are, then choose the correct ones from 2 (autocomplete) fields on their user account page. Note that reps and senators are content on the website and the autocomplete fields reference that content. What is wanted is for the rep and senator to be automatically found and attached to the user based on the location that the user enters. Note that user location is required, because currently that is what is used to determine where "lights" are placed on the map, when a user takes an action.
API to get state legislators: http://docs.openstates.org/en/latest/api/ ...there may be others. Part of this issue may involve rethinking whether location, rep and senator should be attached to the user or to the light. The reason for attaching to the user was so that the information did not have to be entered each time a light was created.
Another possible issue is "why users; why login to post a light?" Two reasons I can think of:

  1. So users can see lights they have posted.
  2. So that location and legislators do not have to be entered to post a light. Changing how this works would be a major restructure. Auto-grabbing state legislators would not.
rick02840 commented 7 years ago

Attached is an image of the current "add light" form, with the addition of location, rep and senator fields (not on the current live site). Is there a way to simply use the location field to get the rep and senator from the API?
Perhaps when the map loads upon location selection the API request is made to fill in those fields?
Other considerations: A. perhaps add user email address to associate the light with the user (for users not logged-in). B. currently, on save, the user sees the Drupal node (light) they just created - that seems decent but I know there are other ideas here: https://github.com/hackforwesternmass/lightupthemap/issues/17 C. if a user IS logged in, the location, rep and senator locations should probably pre-fill from values in the user. IMHO, just adding the functionality to auto-get the rep and senator via and API would be a big thing as a first step. I may not be understanding what has been worked on so far, which involves posting a light via json instead of using the regular Drupal node add form. So, if that is better somehow, then the above may not apply.

create-light---light-up-the-map

a-pasquale commented 7 years ago

We could add the API lookup for representatives to this add light form. What template file is used to generate this form?

rick02840 commented 7 years ago

I believe it is this: 'core/themes/classy/templates/content-edit/node-edit-form.html.twig We can't overwrite that file, but copy it, put it in here: themes/custom/lightup/templates/content/with a different file name that gets called only for the light content type. That might be node--light-edit-form.html.twig ...you could try that. I am not sure if Drupal will pick that up without adding a hook_theme_suggestions_HOOK_alter() ...will look when I get a chance. You have to clear Drupal cache to pick it up any template file changes.

rick02840 commented 7 years ago

I think what you need is ahook_form_alter so I added a module to do that and committed and pushed to branch 11-rest-create-lights

The module contains this:

function lightupthemap_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  // get the form id
  // drupal_set_message($form_id);

  // for node light add form only
  if ($form_id == 'node_light_form') {

    // to spit out the $form object
    // uncomment below, but devel, and devel kint modules must be enabled
    // kint($form);

    $form['field_senator_text']['widget']['0']['#default_value'] = 'this is the senator';
    $form['field_representative_text']['widget']['0']['#default_value'] = "this is the representative";
  }
}

Those last 2 lines you can hopefully work with to add values from the API?

rick02840 commented 7 years ago

Actually, this may be the wrong way to do it. This is going to load those values into those fields when the form loads. I assume what is wanted is to load values into those fields from the API when the location is selected by the user?

It seems like that would be all done by javascript not by php?

Let me know what's needed - maybe we can do a zoom or hangout to go over.

rick02840 commented 7 years ago

I added a template file for the node add / edit: themes/custom/lightup/templates/content-edit/node-edit-form--light.html.twig I put a div with text at top and bottom of it so make it obvious where is starts and stops. In branch 11-rest-create-lights

rick02840 commented 7 years ago

BTW, this is the API? http://docs.openstates.org/en/latest/api/ This looked like it might be helpful: https://drupal.stackexchange.com/questions/207588/how-to-render-data-from-external-api

a-pasquale commented 7 years ago

The functionality is include in the update for issue #11