How to redirect user to API Model Method admin page after saving custom content type?

Not applicable

API Models do not support custom fields, which would be useful for tracking or governance.

To circumvent this issue, a custom content type was created called an API Publishing Form.

In the API Publishing Form, the user provides the model name, description, an OAS (swagger) document and any meta data requested in the form. Upon submission, the form automatically creates the API Model and revision.

The only problem with this setup, it does not render and publish the model's methods (admin/content/smartdocs/models -> select desired model).

We need to redirect the user to the model method admin page but the url path for this page uses a hashed UUID that is not stored in an easy to access field.

Example url path:

admin/content/smartdocs/models/e343420c-946e-437f-b456-4c19ff3f44ab/revisions/1

How can I get the UUID used in the path listed above? What variable stores the value e343420c-946e-437f-b456-4c19ff3f44ab?

Thank you for your time,

Mitchel

Solved Solved
1 3 317
1 ACCEPTED SOLUTION

@Mitchel Skees ,

Great Question, Seems like model UUID is not stored anywhere in Developer Portal database (MySQL). It's actually fetched from Apigee API Modelling data store using APIs.

Below code will help you to fetch all models & their UUIDs related to Developer Portal in a custom module.

  // Register autoloaders.
  drupal_load('module', 'devconnect');
  devconnect_init();
  $config = devconnect_default_org_config();
  if (empty($config->orgName) || empty($config->endpoint)) {
    // Edge connection is not configured, so we bail.
  }

  drupal_load('module', 'smartdocs');

  $model = new Apigee\SmartDocs\Model($config);
  $model_list = $model->listModels();

  foreach ($model_list as $model) {
	var modelUUID = $model->getUuid()
  }

You have also mentioned that on submission you create a new model, that means you should have model UUID in response of API call / Function that created model. So , I don't think you even need above code.

Can you post the custom code using which you are creating new model ? That will give us better picture.

Checkout below code which actually creates smartdocs model & you can also see how UUID is retrieved & redirected to some other page that has UUID reference. I am pretty sure you might be using same logic in your custom module.

function smartdocs_add_model_submit(array $form, array &$form_state) {


  $model = new Model(devconnect_default_org_config());
  $model->setName(trim($form_state['values']['model_name']));
  $model->setDisplayName(trim($form_state['values']['display_name']));
  $model->setDescription(trim($form_state['values']['model_description']));


  $success = FALSE;
  try {
    $model->save();


    // Get the content of the custom template.
    $custom_template_file = variable_get('smartdocs_custom_template_file', NULL);
    $template_content = FALSE;
    if (!empty($custom_template_file)) {
      $file = file_load($custom_template_file);
      $path = FALSE;
      if ($file === FALSE) {
        // The custom template file no longer exists in db, show error and
        // remove setting.
        drupal_set_message(t('Custom model template could not be loaded. Standard model template is being used instead.'), 'warning');
        watchdog('smartdocs', 'Custom model template could not be loaded, smartdocs_custom_template_file variable was set to %smartdocs_custom_template_file.', array('%smartdocs_custom_template_file' => $custom_template_file), WATCHDOG_ERROR);
        variable_del('smartdocs_custom_template_file');
      }
      else {
        $path = file_create_url($file->uri);
      }


      // Get custom template content if path is valid.
      if ($path) {
        $template_content = file_get_contents($path);
        if ($template_content === FALSE) {
          // The custom template file no longer exists in filesystem, show error
          // and remove setting.
          drupal_set_message(t('Custom model template could not be loaded. Standard model template is being used instead.'), 'warning');
          watchdog('smartdocs', 'Custom model template could not be loaded, could not get contents of file %path.', array('path' => $path), WATCHDOG_ERROR);
          variable_del('smartdocs_custom_template_file');
        }
      }
    }


    // Check to see if successfully loaded the custom template and if not get
    // standard template content to add to model.
    if ($template_content === FALSE) {
      $path = drupal_get_path('module', 'smartdocs') . '/templates/smartdocs.hbr';
      $template_content = file_get_contents($path);
    }


    $template = new Template(devconnect_default_org_config(), $model->getUuid());
    $template->save(SMARTDOCS_TEMPLATE_NAME, 'method', $template_content, TRUE);


    $success = TRUE;
    module_invoke_all('smartdocs_model_update', $model->getUuid());
    $form_state['values']['model_id'] = $model->getUuid();


    // Create taxonomy term for model.
    $term = taxonomy_get_term_by_name($model->getName(), 'smartdocs_models');
    if (empty($term)) {
      $vocab = taxonomy_vocabulary_machine_name_load('smartdocs_models');
      if (!empty($vocab)) {
        $term = new stdClass();
        $term->vid = $vocab->vid;
        $term->name = $model->getName();
        $term->description = $model->getDescription();
        $term->field_model_display_name[LANGUAGE_NONE][0]['value'] = $model->getDisplayName();
        taxonomy_term_save($term);
      }
    }
  }
  catch (Exception $e) {
    drupal_set_message(t('There was an error when trying to create the model.'), 'error');
    watchdog_exception('smartdocs', $e);
  }
  if ($success) {
    menu_rebuild();
    drupal_set_message($model->getDisplayName() . ' was created successfully.', 'status');
    $form_state['redirect'] = 'admin/content/smartdocs/models/' . $model->getUuid();
  }
}

Check the last few lines in above code that explains same. Hope it helps. Keep us posted.

View solution in original post

3 REPLIES 3

@Mitchel Skees ,

Great Question, Seems like model UUID is not stored anywhere in Developer Portal database (MySQL). It's actually fetched from Apigee API Modelling data store using APIs.

Below code will help you to fetch all models & their UUIDs related to Developer Portal in a custom module.

  // Register autoloaders.
  drupal_load('module', 'devconnect');
  devconnect_init();
  $config = devconnect_default_org_config();
  if (empty($config->orgName) || empty($config->endpoint)) {
    // Edge connection is not configured, so we bail.
  }

  drupal_load('module', 'smartdocs');

  $model = new Apigee\SmartDocs\Model($config);
  $model_list = $model->listModels();

  foreach ($model_list as $model) {
	var modelUUID = $model->getUuid()
  }

You have also mentioned that on submission you create a new model, that means you should have model UUID in response of API call / Function that created model. So , I don't think you even need above code.

Can you post the custom code using which you are creating new model ? That will give us better picture.

Checkout below code which actually creates smartdocs model & you can also see how UUID is retrieved & redirected to some other page that has UUID reference. I am pretty sure you might be using same logic in your custom module.

function smartdocs_add_model_submit(array $form, array &$form_state) {


  $model = new Model(devconnect_default_org_config());
  $model->setName(trim($form_state['values']['model_name']));
  $model->setDisplayName(trim($form_state['values']['display_name']));
  $model->setDescription(trim($form_state['values']['model_description']));


  $success = FALSE;
  try {
    $model->save();


    // Get the content of the custom template.
    $custom_template_file = variable_get('smartdocs_custom_template_file', NULL);
    $template_content = FALSE;
    if (!empty($custom_template_file)) {
      $file = file_load($custom_template_file);
      $path = FALSE;
      if ($file === FALSE) {
        // The custom template file no longer exists in db, show error and
        // remove setting.
        drupal_set_message(t('Custom model template could not be loaded. Standard model template is being used instead.'), 'warning');
        watchdog('smartdocs', 'Custom model template could not be loaded, smartdocs_custom_template_file variable was set to %smartdocs_custom_template_file.', array('%smartdocs_custom_template_file' => $custom_template_file), WATCHDOG_ERROR);
        variable_del('smartdocs_custom_template_file');
      }
      else {
        $path = file_create_url($file->uri);
      }


      // Get custom template content if path is valid.
      if ($path) {
        $template_content = file_get_contents($path);
        if ($template_content === FALSE) {
          // The custom template file no longer exists in filesystem, show error
          // and remove setting.
          drupal_set_message(t('Custom model template could not be loaded. Standard model template is being used instead.'), 'warning');
          watchdog('smartdocs', 'Custom model template could not be loaded, could not get contents of file %path.', array('path' => $path), WATCHDOG_ERROR);
          variable_del('smartdocs_custom_template_file');
        }
      }
    }


    // Check to see if successfully loaded the custom template and if not get
    // standard template content to add to model.
    if ($template_content === FALSE) {
      $path = drupal_get_path('module', 'smartdocs') . '/templates/smartdocs.hbr';
      $template_content = file_get_contents($path);
    }


    $template = new Template(devconnect_default_org_config(), $model->getUuid());
    $template->save(SMARTDOCS_TEMPLATE_NAME, 'method', $template_content, TRUE);


    $success = TRUE;
    module_invoke_all('smartdocs_model_update', $model->getUuid());
    $form_state['values']['model_id'] = $model->getUuid();


    // Create taxonomy term for model.
    $term = taxonomy_get_term_by_name($model->getName(), 'smartdocs_models');
    if (empty($term)) {
      $vocab = taxonomy_vocabulary_machine_name_load('smartdocs_models');
      if (!empty($vocab)) {
        $term = new stdClass();
        $term->vid = $vocab->vid;
        $term->name = $model->getName();
        $term->description = $model->getDescription();
        $term->field_model_display_name[LANGUAGE_NONE][0]['value'] = $model->getDisplayName();
        taxonomy_term_save($term);
      }
    }
  }
  catch (Exception $e) {
    drupal_set_message(t('There was an error when trying to create the model.'), 'error');
    watchdog_exception('smartdocs', $e);
  }
  if ($success) {
    menu_rebuild();
    drupal_set_message($model->getDisplayName() . ' was created successfully.', 'status');
    $form_state['redirect'] = 'admin/content/smartdocs/models/' . $model->getUuid();
  }
}

Check the last few lines in above code that explains same. Hope it helps. Keep us posted.

Hey @Anil Sagar,

Your advice was a big help. I can't post all the code here since I don't have permission to do so but I can post the following from the hook_form_FORM_ID_alter function:

$loaded_model_method_admin_url = '/admin/content/smartdocs/models/' . $loaded_model->getUuid() . '/revisions/';
$form_set_value_input = array(LANGUAGE_NONE => array(0 => array('url' => $loaded_model_method_admin_url, 'title' => $loaded_model_method_admin_url)));


form_set_value($form['field_method_admin_url'], $form_set_value_input, $form_state);<br>

When the publishing form is saved the custom

hook_form_FORM_ID_alter function is called running the code above which saves the url in the field "Method Admin URL"

Then a rule redirects the user to the url stored in the token [node:field_method_admin_url].

This way the author of the publishing form can manually select which methods to render and publish once the model has been created.

Thank you for your time,

Mitchel

Awesome, Glad to hear that your issue is resolved. Thanks for the updates @Mitchel Skees , Keep us posted in future if you have any queries.