0

I' trying to create custom module that should add comment to a content type in a modal window, with ajax. I wrote following code, but when I click on the 'Add review' link, browser loads new page when I can add 'reviews about shop' content type. I want to do this in a modal window, without reloading

function current_posts_menu() {
 $items['add/%ctools_js/review'] = array(
   'page callback'    => 'current_posts_ctools_modal_review',
   'page arguments'   => array(1),
   'access arguments' => array('create review content'),
 );
 return $items;
}

function current_posts_popup_style() {

  static $added = FALSE;
  if ($added == FALSE) {
    $added = TRUE;

    // Include the CTools tools that we need.
    ctools_include('modal');
    ctools_include('ajax');
    ctools_modal_add_js();


    $popup_style = array(
      'first-popup-style' => array(
        'modalSize' => array(
          'type' => 'fixed', 
          'width' => 420,  
          'height' => 'auto', 
        ),
        'modalOptions' => array(
          'opacity' => (float) 0.3, .
          'background-color' => '#000000', 
        ),
        'closeText' => '', 
        'loadingText' => '', .
        'animation' => 'fadeIn', 
        'animationSpeed' => 'fast', 
      ),
    );


    drupal_add_js($popup_style, 'setting');

  }
}
function current_posts_block_info(){
 $blocks['add_review'] = array(
   'info' => t('Add review'),
 );
 return $blocks;
}

/**
* Implements hook_block_view().
*/
function current_posts_block_view($delta = '') {
    $block = array();
 switch ($delta) {
   case 'add_review':
     ctools_include('ajax');
     ctools_include('modal');
     ctools_modal_add_js();
     current_posts_popup_style();
     $comment_site_id = arg(1);
      $block['content'] = l(t('Add a review'), "add/nojs/review/{$comment_site_id }", array('html' => TRUE, 'attributes' => array('class' => "ctools-use-modal ctools-modal-first-popup-style")));
/*ctools-modal-first-popup-style */
     break;
 }
 return $block;
}

function current_posts_form_alter(&$form, &$form_state, $form_id) {
 switch ($form_id) {
   case 'comment_site':
     $comment_site_id = arg(3);
     if (is_numeric(arg(3))) {
       $lang = field_language('node', $form['#node'], 'field_news');
       // Declaration the default value for field on the comment creating form.
       $form['field_news'][$lang][0]['nid']['#default_value'] = $comment_site_id;

     }

 }
}

function current_posts_ctools_modal_review($js = FALSE) {
 // loading libraries ctools'a
 ctools_include('node.pages', 'node', '');
 ctools_include('modal');
 ctools_include('ajax');

 $type = 'reviews_about_shop';
 $node = (object) array(
   'uid' => $user->uid,
   'name' => (isset($user->name) ? $user->name : ''),
   'type' => $type,
   'language' => LANGUAGE_NONE
   );
  // Loading node form with disabled scripts.
 if (!$js) {
   return drupal_get_form($type . '_node_form', $node);
}
 // Create array $form_state.
 $form_state = array(
   'title' => t('Add Review'),
   'ajax'  => TRUE,
 );

    $form_state['build_info']['args'] = array($node);
    $output = ctools_modal_form_wrapper($type . '_node_form', $form_state);
    // Actions after form submit.
    if (!empty($form_state['executed'])) {
    $output = array();
    // Close pop up.
    $output[] = ctools_modal_command_dismiss();
    $comment_site_id = arg(3);
    // Insert  updated  view into a  page.
    if (is_numeric(arg(3))) {
     $output[] = ajax_command_html('.latest-reviews-full-style', views_embed_view('reviews_on_main_page', 'block', arg(3)));
    }
 }
    print ajax_render($output);
}

Note: I need like adding content type in a modal window. In my module reviews_about_shop is a content type that linked to a content type comment_site.

1

You need to add ajax library to that

drupal_add_library('system', 'drupal.ajax');

Or you can use the below function to render the ajax link

$block['content'] = l(t('Add a review'), "add/nojs/review/{$comment_site_id }", array('html' => TRUE, 'attributes' => array('class' => "ctools-use-modal ctools-modal-first-popup-style")));

Replace the above line to below.

$block['content'] = ctools_modal_text_button(t('Newsletter datacapture modal'), "add/nojs/review/{$comment_site_id }", t('Add a review'), "ctools-modal-first-popup-style");

Modify the argument

function current_posts_menu() {
 $items['add/%ctools_js/review/%'] = array(
   'page callback'    => 'current_posts_ctools_modal_review',
   'page arguments'   => array(1,3),
   'access arguments' => array('create review content'),
 );
 return $items;
}

and not to mention "Clear Cache"

  • doesn't work, it loads new page – marky Aug 22 '16 at 8:59
  • check whether "ctools-use-modal" class is added to the link!! – NTT Aug 22 '16 at 9:02
  • added, same result – marky Aug 22 '16 at 9:09
  • My Question is whether it is added automatically or not? – NTT Aug 22 '16 at 9:10
  • yes. it is added automatically – marky Aug 22 '16 at 9:16
0

Assuming you're on Drupal 7. Alternatively, you could do it with the Modal Forms (with ctools) module

Modal forms make use of the modal feature in the ctools module to open some common forms in a modal window.

  • it is a little bit not what I need – marky Aug 22 '16 at 9:50
  • I need like adding content type in a modal window. In my module reviews_about_shop is a content type that linked to a content type comment_site – marky Aug 22 '16 at 9:52

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.