3

I have created a controller called myPlugin_Controller.php

This is the following code inside it.

 <?php
class myPluginController extends BaseController
{
    public function actionSave()
    {

        $this->requirePostRequest();

        // get all the post variables
        $elementId = craft()->request->getPost('elementId');
        $userId = craft()->request->getPost('userId');

        // set the name of the field you want to update
        $fieldHandle = 'friends';

        // based on the element and field vars, get the models for each
        // using the elements and fields services
        $element = craft()->elements->getElementById($elementId);
        $field = craft()->fields->getFieldByHandle($fieldHandle);

        // store the current ids into a variable
        $currentIds = $element->{$field->handle}->ids();

        // merge the new id into the current ids
        $newIds = array_merge($currentIds, (array) $userId); 

        // save the new array of ids, using the relations service
        craft()->relations->saveRelations($field, $element, $newIds);

        // redirect to the redirect url
        $this->redirectToPostedUrl();

    }
}
?>

This is my code in an html file, it is submitting some data however it returns a 404 error.

   <form method="post" accept-charset="UTF-8"> 
                  {{ getCsrfInput() }}
              <input type="hidden" name="action" value="myPlugin/Save">
              <input type="hidden" name="redirect" value="/success">

              <input type="hidden" name="elementId" value="{{ user.id }}">

              <input type="hidden" name="userId" value="{{ currentUser.id }}">

              <button type="submit" >Submit</button>
          </form>

i'm not too sure where its going wrong now.

Also if your wondering why its called user this is why {% set user = craft.users.username( craft.request.segment(2) ).first() %} Do you know where I am going wrong now any tips or help is all appreciated! :)

Still not working I have looked at craft cms documentation online i've followed your steps and there steps.

Is there anything you can still see that is wrong?

3

You will need a custom plugin to do this.

I'll assume that you're familiar with that process.

You can build a form in your template like this, passing in the entry ID you want to add the user to and the user ID you want to add.

<form method="post" accept-charset="UTF-8"> 
    {{ getCsrfInput() }}
    <input type="hidden" name="action" value="myPlugin/save">
    <input type="hidden" name="redirect" value="/success">

    <input type="hidden" name="elementId" value="{{ entry.id }}">

    <input type="hidden" name="userId" value="{{ currentUser.id }}">

    <button type="submit" >Submit</button>
</form>

Then, you'll need a controller action in your plugin to which you submit a POST request from a form.

public function actionSave()
{
    $this->requirePostRequest();

    // get all the post variables
    $elementId = craft()->request->getPost('elementId');
    $userId = craft()->request->getPost('userId');

    // set the name of the field you want to update
    $fieldHandle = 'myUserField';

    // based on the element and field vars, get the models for each
    // using the elements and fields services
    $element = craft()->elements->getElementById($elementId);
    $field = craft()->fields->getFieldByHandle($fieldHandle);

    // store the current ids into a variable
    $currentIds = $element->{$field->handle}->ids();

    // merge the new id into the current ids
    $newIds = array_merge($currentIds, (array) $userId); 

    // save the new array of ids, using the relations service
    craft()->relations->saveRelations($field, $element, $newIds);

    // redirect to the redirect url
    $this->redirectToPostedUrl();

}

EDIT:

It's very important that you follow the naming conventions in the docs.

You controller class should be named MyPluginController, which would then make the action in the form myPlugin/save (controller and action get removed from the exposed plugin and action names).

Also the redirect should be a page on the side where you want the user to go after being added, not pointing to your plugin.

0
0

Hi Jeremy thanks for the help it is getting me on the right track what you have given me thanks :), However I still haven't quite solved it, I found documentation on how to use custom controllers. This is where I am at now.

I have created a controller under the controllers folder in the plugins directory with the file name of myPlugin_myController.php

This is the following code inside it.

<?php
class myPlugin_myController extends BaseController
{
    public function actionSave()
    {

        $this->requirePostRequest();

        // get all the post variables
        $elementId = craft()->request->getPost('elementId');
        $userId = craft()->request->getPost('userId');

        // set the name of the field you want to update
        $fieldHandle = 'friends';

       // based on the element and field vars, get the models for each
       // using the elements and fields services
       $element = craft()->elements->getElementById($elementId);
       $field = craft()->fields->getFieldByHandle($fieldHandle);

       // store the current ids into a variable
       $currentIds = $element->{$field->handle}->ids();

       // merge the new id into the current ids
       $newIds = array_merge($currentIds, (array) $userId); 

       // save the new array of ids, using the relations service
       craft()->relations->saveRelations($field, $element, $newIds);

       // redirect to the redirect url
       $this->redirectToPostedUrl();

    }
}
?>

This is my code in an html file, it is submitting some data however it returns a 404 error.

 <form method="post" accept-charset="UTF-8"> 
              {{ getCsrfInput() }}
              <input type="hidden" name="action" value="myPlugin/myController/actionSave">
              <input type="hidden" name="redirect" value="myPlugin/myController">

              <input type="hidden" name="elementId" value="{{ entry.id }}">

              <input type="hidden" name="userId" value="{{ currentUser.id }}">

              <button type="submit" >Submit</button>
          </form>

Do you know where I am going wrong now any tips or help is all appreciated! :)

8
  • You controller should be named MyPluginController, which would then make the action in the form myPlugin/save (controller and action get removed from the exposed action). Also the redirect should be a page on the side where you want the user to go after being added, not pointing to your plugin. Apr 10 '17 at 11:07
  • Also, it'd be best to update your question with this, so that we can better help you. Apr 10 '17 at 11:08
  • 3
    @Alistair: This isn't an answer, it's an extension of your original question. Please edit this information into the original question... we'll be deleting this non-answer.
    – Lindsey D
    Apr 10 '17 at 17:49
  • @JeremyGimbel I have updated the code up the top, still no luck :/ but I think we are getting closer.
    – Alistair
    Apr 12 '17 at 9:17
  • 1
    Based on the code in your question, you still haven't followed the correct naming conventions. Capitalization matters. This would very much prevent the controller from executing. I would suggest reviewing the docs (craftcms.com/docs/plugins/controllers) or perhaps examining some of the other available plugins (perhaps P&T's own contact form plugin) to compare your naming and structure. Apr 14 '17 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.