Jump to content

Batch deleting Fields and Templates


owzim
 Share

Recommended Posts

I am using a PW installation as a template for every future PW installation. So within that I create all kinds of fields and templates for many different use cases which might not be used later in a copy installation so I delete all unused.

What would be a good way to delete them fast instead of clicking every single template and field and delete it in the delete tab?

Link to comment
Share on other sites

Ditto, I have created some routine in a module to create fields and templates via API which would have taken maybe 400 lines of code to 100.

https://gist.github.com/somatonic/6272112

To delete the fields and template from this it's also easy.

I know there will be soon something based on array or json/yaml to create fields and export/import those things more easy. But just maybe find it useful.

  • Like 5
Link to comment
Share on other sites

Quick and dirty interface to batch delete unused templates by selecting them with checkboxes:

if ($input->post->submit) {
	foreach ($input->post as $t) {
		// proceed only if the input is an integer different from 0
		if (!(int)$t) return;
		$t = $templates->get($t);
		$templates->delete($t);
		$name = $t->name;
		// delete the fieldgroup associated with this template. more info in the next post
		$fg = $fieldgroups->get($name);
		$fieldgroups->delete($fg);
                // verify that the template is not there and print the confirmation
		if (!$templates->get($t)) echo "<p>template {$name} was deleted.</p>";
	}
} else {
	// print the form
	echo "<form method='post'>";
	echo "<ul>";
	foreach ($templates as $t) {
		// name of the temlate and number of pages it uses
		echo "<li>" . $t->name . " (" . $t->getNumPages() . " pages)";
		// include checkbox if the template is not used by pages, and set it's id as value
		if (!$t->getNumPages()) echo " <input type='checkbox' value='{$t->id}' name='{$t->name}'>";
		echo "</li>";
	}
	echo "</ul>";
	echo "<input type='submit' value='delete these' name='submit'>";
	echo "</form>";
} 

 
Edit: Added "if ($templates->get($t)))" to the "template was deleted" line. Now we are sure that it was really deleted :)

Edit2: Edited the code based on the problem explained in the next post

  • Like 10
Link to comment
Share on other sites

I run into a problem with the code that I posted above. After deleting a template via the API, I can't create a new one with the same name, and get this error message: You must save Fieldgroup 'x' before adding to Template 'x'

This seems to be the same problem that Martijn had here: http://processwire.com/talk/topic/3962-you-must-save-fieldgroup-markupcsscompress-before-adding-to-template-markupcsscompress/, and modifying the code above to delete the fieldgroup with the same name as the template (besides the template itself) seems to solve the problem.

  • Like 1
Link to comment
Share on other sites

I really like that I can visually compose templates and fields with all the widths and so on so the subtractive approach is the way to go for me at the moment, thanks diogo for the quick but useful code.

netcarver and soma, I will definitely dig deeper into that approach as well since it's so nicely automated, thanks.

Link to comment
Share on other sites

  • 10 months later...
  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...