1

Module name, 'text_grabber' Content Type, 'node' name 'essay' **When I enable this module, I want a checkbox to be added to the ~/node/add/essay page. So when an user adds a new essay to the site they can select yes on the checkbox that will enable the text_grabber module for that specific essay. If the user selects yes, I want to be able to add a js script specific to that essay before it is saved. This will allow me to execute code if event is triggered. Is this possible.

I think I can add a checkbox using an .install like mentioned here: Using field_create_field and field_create_instance to add a checkbox to a content type

but the other part, what can I do?? In layman's terms, I am not tremendously experienced.

1

You can add a field in a number of ways:

  1. Edit the content type and add a Boolean field through the UI
  2. In your .install file as you already mentioned
  3. Using hook_form_alter() to add a #checkbox.

If you used option 1 or 2 you can pick up whether the field is checked or not using hook_node_presave() or hook_node_update().

If you used option 3 you'll probably need to add a #submit value to the form which calls a custom function in your module.

5
  • So If I want to add text to the body would it be: function text_grabber_node_presave($essay) { if (isset($essay->checkbox)) { $essay->body += "text has been grabbed"; } Am I not returning anything or ending the function with node_save()? Do i put $node or putting the name of the node is correct?
    – QueenN2Dev
    May 5 '15 at 5:18
  • You use $node rather than $essay, and you don't need to return the node, any edits you make will be carried through.
    – Darvanen
    May 5 '15 at 14:41
  • what if I want to add js to a page where they select the checkbox? I will still use hook_node_presave()? Will I put if(they checked box) { drupal_add_js(path/to/js, 'filename.js')} <<<WOULD THIS BE THE ONLY CODE NEEDED?? Would i need to do anything else because it is not working. no js on page. Also how do you find the exact index for the form field? I can't just vardump, so Im guessing i know it has [0][value] etc on the name of the input. Am I using this as the index syntax??
    – QueenN2Dev
    May 7 '15 at 3:02
  • Use dpm($variable_name) in your php to inspect variables, prints to screen and required devel module. If you're using js you should put the drupal_add_js in a hook_form_alter() function instead of presave. I think you need to do some more research, and then ask new questions rather than tacking them on here.
    – Darvanen
    May 7 '15 at 5:44
  • 90% of all research I've done was jargon I can't understand. I look on places like here for quality answers, most people answer with links and essay explanation with no real answers. If there were solid answers or EXAMPLES for reference that would be great. I have not found any other way of getting answers(have yet to get this simple process done) besides asking questions myself.
    – QueenN2Dev
    May 7 '15 at 15:30
0

This created the fields on the table and showed up on the edit page (started file with opening php tag) "custom_module.install"

function custom_module_install() {
  field_create_field(array(
     'field_name'  => "text_grabber_enable",
     'type'        => 'list_boolean',
     'cardinality' => 1,
     'settings'    => array(
        'allowed_values' => array( 
          0 => '',
          1 => '',
         ),
      ),
  ));
  field_create_instance(array(
     'field_name'  => "text_grabber_enable",
     'entity_type' => 'node',            
     'bundle'      => 'video',
     'label'       => t('Enable Text Grabber'),
     'required'    => false,                       
     'widget'      => array(
        'type'     => 'options_onoff',              
        'settings' => array(
           'display_label' => 1,                     
        ),
      ),
  ));
}

Below is inside my custom_module.module file. Hook node view changes what happens when a certain node type is loaded from what I understand. I used the Devel module to view exactly where the value was in the text_grabber_enable array of the node object (ex. dpm($node); within the node view function during testing)

function custom_module_node_view($node, $view_mode) {
    if($node->type == 'essay' && $node->text_grabber_enable[$node->language][0]['value']) {
        drupal_add_js('(function($) {alert("just a test but text grabber is enabled for this node"); }(jQuery));', array(
           'type' => 'inline',
           'scope' => 'footer',
           'weight' => 5, 
           )
        );
     }
 }

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.