0
votes

sorry if this has been asked before, I looked around but haven't found this specific question on StackOverFlow.com.

I have a view called 'view-post-wall' which I'm trying to add the form that submits posts to this view called 'post' via ajax submit, though I haven't begun adding ajax yet.

My module's name is 'friendicate'

I don't understand what I'm missing here, I'm following a tutorial and have been unable to get past this issue for 2 days now.

I don't get any errors either.

Here is the module code in full


    function _form_post_ajax_add() {
      $form = array();
      $form['title'] = array(
        '#type' => 'textfield',
        '#title' => 'Title of post',
      );
      $form['body'] = array(
        '#type' => 'textarea',
        '#title' => 'description',
      );
      $form['submit'] = array(
          '#type' => 'submit',
          '#value' => 'Submit post',
      );
      return $form;
    }
    function post_ajax_preprocess_page(&$variables) {
      //krumo($variables);
      $arg = arg();
      if($arg[0] == 'view-post-wall') {
          $variables['page']['content']['system_main']['main']['#markup'] = drupal_render(drupal_get_form('_form_post_ajax_add'));
      }
    }

2
Your modules name is friendicate as you said, so the page preprocess should be friendicate_preprocess_page Beside that you can consider addaing the code to the view in the header or footer of the view if you have php filter enabledSteff
sorry, wrong field...Steff

2 Answers

1
votes

There are multiple ways to accomplish this, and I'll outline those methods below. Also, if nothing works from my suggestions below, it's possible that you have an invalid form function name. Im not sure if that throws off Drupal or not. The correct format for the function name should end in _form and contain the arguments $form and $form_state, like so:

_form_post_ajax_add_form($form, &$form_state) { ... }

Also, if you want to use a hook, Steff mentioned in a comment to your question that you'll need to use your module name in the function name.

friendicate_preprocess_page(&$variables) { ... }

Ok, now for a few ideas how to get the form on the page.

Block

You can create a custom block within your module, and then assign it to a region in admin/structure/blocks

<?php


/**
 * Implements hook_block_info().
 */
function friendicate_block_info() {
  $blocks = array();

  $blocks['post_ajax'] = array(
    'info' => t('Translation Set Links'),
    'cache' => DRUPAL_NO_CACHE,
  );

  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function friendicate_block_view($delta = '') {
  $block = array();

  if ($delta == 'post_ajax') {
    $form = drupal_get_form('_form_post_ajax_add_form');
    $block['content'] = $form;
  }

  return $block;
}

Clear the cache and your block should appear in admin/structure/blocks

Views attachment before/after

You can add markup before and after a view using the Views hook hook_views_pre_render()

<?php

/**
 * Implements hook_view_pre_render().
 */
function frendicate_views_pre_render(&$view) {
  if($view->name == 'view_post_wall') { // the machine name of your view
    $form = drupal_get_form('_form_post_ajax_add_form');
    $view->attachment_before = render($form);
  }
}
0
votes

Or maybe use view post render

function friendicate_views_post_render(&$view, &$output, &$cache) { //use the machine name of your view if ($view->name == 'view_post_wall') { $output .= drupal_render(drupal_get_form('_form_post_ajax_add')); } }